/
server.ts
79 lines (65 loc) · 2 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
import * as express from 'express';
import * as partialResponse from 'express-partial-response';
import * as path from 'path';
import {
swaggerify,
secureApp,
configLogging,
configMetrics,
configHealthChecks,
addCompression
} from './config';
import { IOCContainer } from './config/ioc_config';
import { InversifyExpressServer } from 'inversify-express-utils';
const responseTime = require('response-time');
/**
* Node Express Server setup and configuration
*/
export default class ExpressServer {
public server: InversifyExpressServer;
constructor() {
let root: string;
// Setup application root
root =
process.env.NODE_ENV === 'development'
? path.normalize(__dirname + '/../..')
: path.normalize('.');
const container = IOCContainer.getInstance().getContainer();
this.server = new InversifyExpressServer(container, undefined, {
rootPath: '/api/v1'
});
this.server.setConfig(app => {
// Add security configuration
secureApp(app);
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
// Add public folder
app.use(express.static(`${root}/public`));
// Add response time support
// This will add x-response-time to the response headers
app.use(responseTime({ suffix: false }));
// Add partial response support
app.use(partialResponse());
// Add logging configuration
configLogging(app);
// Add metrics configuration
configMetrics(app);
// Configure Healthchecks
configHealthChecks(app);
// Add Compression support
addCompression(app);
// Add swagger support
swaggerify(app);
});
}
public getServer = (): InversifyExpressServer => {
return this.server;
};
}