forked from sonufrienko/microservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
48 lines (42 loc) · 1.21 KB
/
app.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
const http = require('http');
const app = require('express')();
const compression = require('compression');
const bodyParser = require('body-parser');
const winston = require('winston');
const { errors } = require('celebrate');
const mongo = require('./db/mongo');
const authorization = require('./routes/middlewares/authorization');
const logger = require('./routes/middlewares/logger');
const routes = require('./routes/routes');
// Enable KeepAlive to speed up HTTP requests to another microservices
http.globalAgent.keepAlive = true;
const { PORT, NODE_ENV } = process.env;
app.disable('x-powered-by');
app.use(compression());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/healthcheck', (req, res) => {
try {
res.send({
uptime: Math.round(process.uptime()),
message: 'OK',
timestamp: Date.now(),
mongodb: mongo.isConnected()
});
} catch (e) {
res.status(503).end();
}
});
app.use(authorization);
app.use('/v1', routes);
app.use(errors());
app.use(logger);
if (NODE_ENV !== 'test') {
(async () => {
await mongo.connectWithRetry();
app.listen(PORT, () => {
winston.info(`Server listening on http://localhost:${PORT}`);
});
})();
}
module.exports = app;