-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
91 lines (79 loc) · 2.07 KB
/
server.ts
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
import * as restify from 'restify';
import * as bunyan from 'bunyan';
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
}
]
});
export const server = restify.createServer({
name: 'Restify server',
log: logServer,
version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
/**
* 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, // it's a /build directory.
directory: __dirname + '/../',
default: 'index.html'
}));
/**
* Mocking data API.
*/
const dataDir = './data';
server.get('/api/properties', restify.serveStatic({
directory: dataDir,
file: 'sampleData.json'
}));
/*
server.get('/endpoint', function (req, res, next) {
const log = req.log;
log.debug({ params: req.params }, 'request params:');
let json = {result: 'ok'};
res.json(200, json);
});
*/
/**
* Starting server
*/
const cfgPort = 8000; //config.get('port') || 8000;
server.listen(cfgPort, function () {
logServer.info('%s listening at localhost:%s', server.name, cfgPort);
logServer.info('Open UI in browser at http://localhost:8000');
});