-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·67 lines (50 loc) · 1.95 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
'use strict';
global.express = require('express');
global.expressValidator = require('express-validator');
global.helmet = require('helmet'); // security package
global.BASE_PATH = __dirname;
// configuration file
global.config = require('./configuration/config');
global.Constants = require('./utils/Constants');
global.helpers = require('./utils/Helper');
global.messages = require('./utils/messages');
global.underscoreObj = require('underscore');
const app = express(); // create an instance of an express
const SwaggerApiDoc = require('./utils/SwaggerApiDoc');
new SwaggerApiDoc(app);
console.info(`Application is runnign at port :${config.get('server.port')}`);
console.info(`Swagger Doc is running at :${config.get('server.host')}:${config.get('server.port')}/api-docs/#/`);
app.use(express.json({ limit: '1000mb' }));
// app.use(express.urlencoded({ limit: '1000mb' }))
app.use(helmet());
app.disable('x-powered-by');
app.disable('etag'); // this will disable etag to restrict 304 status
app.use('/api/v1/', require('./routes/index'));
// catch 404 and forward to error handler
app.use((req, res, next) => {
var err = new Error('Bad Request');
err.status = Constants.httpStatusCode().NOT_FOUND;
next(err);
});
function exitHandler () {
console.error(__filename, '', 'Exit handler');
process.exit(1);
}
// Node JS event signal for exception handling
[ 'exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException',
'SIGTERM' ].forEach((eventType) => {
process.on(eventType, exitHandler.bind());
});
// any missed exception handle will handle here
app.use((err, req, res, next) => {
console.error(__filename, '', 'last error handled', err.stack);
console.debug(__filename, 'server error handle', req.method, req.url);
var statusCode = err.status || Constants.httpStatusCode().SERVER_ERROR;
helpers.createResponse(res, statusCode,
messages.INTERNAL_SERVER_ERROR,
{
error: messages.INTERNAL_SERVER_ERROR
}
);
});
module.exports = app;