-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathserver.js
80 lines (59 loc) · 1.5 KB
/
server.js
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
const http = require('http');
const express = require('express');
const helmet = require('helmet');
const compression = require('compression');
const {port} = require('./settings/server/config.js');
const contentSecurityPolicy = require('./settings/server/content-security-policy.js');
const csie = require('./routes/urls.js');
const apis = require('./apis/urls.js');
/**
* Create HTTP server instance.
*/
const server = express();
const httpServer = http.createServer(server);
/**
* Start HTTP server.
*/
httpServer.listen(port);
/**
* Remove default express HTTP response header `x-powered-by`.
*/
server.set('x-powered-by', false);
/**
* Put easter egg in HTTP response header.
*/
server.use(({}, res, next) => {
res.set('x-powered-by', 'toolbuddy');
next();
});
/**
* Set `Content-Security-Policy-Report-Only` header.
* Settings can be found at `settings/server/content-security-policy`.
* Don't change this unless you know what you are doing.
* @todo reportOnly: false.
*/
server.use(helmet.contentSecurityPolicy({
directives: contentSecurityPolicy(),
loose: false,
reportOnly: true,
}));
/**
* Compress all HTTP response body using gzip.
* Use this to minimize transmit data.
*/
server.use(compression());
/**
* Setup web api routes.
*/
server.use('/api', apis);
/**
* Setup web page routes.
*/
server.use('/', csie);
/**
* Setup error handler.
*/
server.use((error, {}, res, {}) => {
console.error(error);
res.status(error.status || 500).send(error.message);
});