-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathapp.js
81 lines (74 loc) · 2.08 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
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
const createError = require('http-errors');
const express = require('express');
const logger = require('morgan');
const config = require('./config');
const healthCheck = require('express-healthcheck');
const pkg = require('./package');
//-------------------------------------
//
// setup the app
//
//-------------------------------------
const app = express();
app.use(logger('dev'));
app.set('json spaces', 2);
app.set('trust proxy', true);
app.set('case sensitive routing', true);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const healthy = true;
const ready = true;
//-------------------------------------
//
// setup some routes for us to hit and
// other helpful routes when working with kubernetes
//
//-------------------------------------
app.use('/', require('./routes'));
app.use('/healthcheck', healthCheck());
app.use('/readiness', function (req, res, next) {
res.status(200).json({ ready });
});
app.get('/version', function (req, res, next) {
const version = pkg.version;
res.status(200).json({ version });
});
process.on('SIGTERM', function () {
// cleanup environment, this is about to be shut down
process.exit(0);
});
//-------------------------------------
//
// catch 404 and forward to error handler
//
//-------------------------------------
app.use(function (req, res, next) {
next(createError(404));
});
//-------------------------------------
//
// error handler
//
//-------------------------------------
app.use(function (err, req, res, next) {
// set locals, only providing error in development
err.response = {
message: err.message,
internalCode: err.code
};
// render the error page
res.status(err.status || 500).json(err.response);
});
//-------------------------------------
//
// create server if this is main
//
//-------------------------------------
if (module === require.main) {
// Start the server
const server = app.listen(config.get('PORT'), () => {
const port = server.address().port;
console.log(`App listening on port ${port}`);
});
}
module.exports = app;