-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
151 lines (134 loc) · 4.08 KB
/
Copy pathserver.ts
File metadata and controls
151 lines (134 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as restify from 'restify';
import * as bunyan from 'bunyan';
import { createLiner as getLinerInstance } from './src/liner';
import {
db,
stmtInsertContact
} from './src/db';
const logServer = bunyan.createLogger({
name: 'serverLog',
streams: [
{
level: 'debug',
stream: process.stdout
},
{
level: 'debug',
path: './logs/server.log',
type: 'rotating-file',
period: '1d', // daily rotation
count: 10 // keep this number of back copies
}
]
});
//logServer.info("process.env.NODE_ENV = %s", process.env.NODE_ENV);
export const server = restify.createServer({
name: 'CSV Processing Demo',
log: logServer,
version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser({
multipartFileHandler: function (part, req) {
const liner = getLinerInstance();
part.pipe(liner);
let numLines = 0;
liner.on('readable', function () {
let line;
while (null !== (line = liner.read())) {
numLines++;
//console.log( line );
// line processing
let values = line.split(',');
stmtInsertContact.run(values[0], values[1], values[2]);
}
});
part.on('end', () => {
logServer.debug(`Uploaded lines, totally: ${numLines}`);
});
}
}));
/**
* Request handling before routing.
* Note that req.params will be undefined, as that's filled in after routing.
*/
server.pre(function (req, res, next) {
const log = req.log;
log.info(`${req.method.toUpperCase()} ${req.url}`);
//log.debug({ headers: req.headers }, 'req.Headers:');
next();
});
/**
* Utility endpoint.
*/
server.get('/health', function (req, res, next) {
res.json(200, { 'health': 'ok' });
return next();
});
/**
* Mapping static resources
*/
server.get('/', restify.serveStatic({
directory: './public',
file: 'index.html'
}));
server.get(/\/public\/?.*/, restify.serveStatic({
directory: __dirname,
default: 'index.html'
}));
/**
* File processing
*/
server.post('/upload', function (req, res, next) {
const log = req.log;
/*
log.debug({ headers: req.headers }, 'req.Headers:');
log.debug({ params: req.params }, 'req.Params:');
log.debug({ files: req.files }, 'req.Files:');
//log.debug({ request: req }, 'Request:');
*/
//log.debug(`isUpload ? ${req.isUpload()}`);
//log.debug(`req.files: ${req.files}`);
log.info('Upload completed');
const body = '<!DOCTYPE html><body><p>Upload completed.</p><br/>See saved contacts: <a href="/contacts">/contacts</a></body>';
res.writeHead(202, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/html'
});
res.write(body);
res.end();
});
/**
* List of saved contacts.
*/
server.get('/contacts', function (req, res, next) {
let output = '';
db.serialize(function () {
db.each("SELECT rowid AS id, firstname, lastname, email FROM contacts",
// each row callback
function (err, row) {
//console.log(`${row.id}: ${row.firstname} ${row.lastname} - ${row.email}`);
output += `${row.id}: ${row.firstname} ${row.lastname} - ${row.email}`;
},
// complete callback
function (err, rowsNum) {
const body = `<!DOCTYPE html><p>retrieved rows: ${rowsNum}</p><pre>${output}</pre>`;
res.writeHead(200, {
'retrieved-rows-number': rowsNum,
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/html'
});
res.write(body);
res.end();
}
);
});
});
/**
* Starting server
*/
const cfgPort = 8000; //config.get('port') || 8000;
server.listen(cfgPort, function () {
logServer.info('%s listening at %s', server.name, server.url);
});