-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (38 loc) · 1.2 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
// Copyright © 2016-2024 Endless OS Foundation LLC
// SPDX-License-Identifier: GPL-2.0-or-later
// vim: ts=2 sw=2 expandtab
'use strict';
const bodyParser = require('body-parser');
const express = require('express');
const api = require('./api');
const config = require('./config');
const app = express();
const logger = config.logger;
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// XXX: For tests we need to be able to spoof IPs and
// for prod/staging we are behind NGINX
app.enable('trust proxy');
app.use(api.router);
// Error handler
app.use((err, req, res, next) => {
logger.error(`while handling ${req.method} ${req.url}: ${err}`);
if (res.headersSent) {
return next(err);
}
let errorMessage = "Server error";
if (process.env.NODE_ENV == 'test') {
errorMessage = err.stack;
}
res.status(500)
.json({ error: errorMessage,
success: false });
});
logger.info('Server starting on ' +
config.server_bind_address + ':' + config.server_port + '...');
app.listen(config.server_port, config.server_bind_address, () => {
logger.info('Server started (' + process.env.NODE_ENV + ')');
});