-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
120 lines (97 loc) · 2.64 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const http = require('http');
const config = require('./src/config');
const { sequelize, models } = require('./src/models');
const routes = require('./src/routes');
const responses = require('./src/responses');
const app = express();
app.enable('trust proxy');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors(config.cors));
const port = parseInt(process.env.PORT || config.port, 10); // Get port from environment
/* global */
global.config = config;
global.models = models;
global.sequelize = sequelize;
/* global */
/* configure port to app */
app.set('port', port);
/* Attaching custom response methods to res object */
app.use(responses);
/* configure routes */
routes(app);
// Make sure k8s health check does not fail
app.get('/healthz', (req, res) => {
res.sendStatus(200);
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use((error, req, res) => {
console.error(error);
res.status(error.status || 500);
res.json({
message: "Oops! Couldn't perform this action at the moment. Please try again",
error
});
});
/**
* Create HTTP server.
*/
const server = http.createServer(app);
const isTest = process.env.NODE_ENV === 'test';
const isProduction = process.env.NODE_ENV === 'production';
/**
* Bind onError and onListening handler
*/
server.on('error', error => {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(`${bind} requires elevated privileges`); // eslint-disable-line no-console
process.exit(1);
break;
case 'EADDRINUSE':
console.error(`${bind} is already in use`); // eslint-disable-line no-console
process.exit(1);
break;
default:
throw error;
}
});
server.on('listening', () => {
const addr = server.address();
const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
console.log(`Listening on ${bind}`);
});
function start(done) {
sequelize.sync({ force: isTest }).then(async () => {
if (isTest || isProduction) {
// Setup testing/prod environment
}
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port, done);
});
}
function lower(done) {
server.close(done);
}
module.exports = {
lower,
start,
sequelize,
models,
default: app
};