This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathapp.js
109 lines (89 loc) · 3.16 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
require('newrelic');
const bodyParser = require('body-parser');
const compression = require('compression');
const getConfig = require("./config");
const cookieParser = require('cookie-parser');
const cors = require('cors');
const express = require("express");
const { getApiRoutes } = require('./routes');
const helmet = require('helmet');
const Logger = require("./utils/logger");
const path = require("path");
const RateLimit = require('express-rate-limit');
const addRequestId = require('express-request-id')();
/* eslint-disable */
const pug = require("pug");
const favicon = require('serve-favicon');
/* eslint-enable */
/* ------------------------------------------------------------------ *
API CONFIG
* ------------------------------------------------------------------ */
const config = getConfig(process.env.NODE_ENV);
const logger = new Logger({name: "code-gov-api", level: config.LOGGER_LEVEL});
const app = express();
app.set('json escape', true);
if( config.USE_RATE_LIMITER) {
const limiter = new RateLimit({
windowMs: parseInt(process.env.WINDOW_MS || 60000, 10),
max: parseInt(process.env.MAX_IP_REQUESTS || 500, 10),
delayMs:parseInt(process.env.DELAY_MS || 0, 10),
headers: true
});
app.use(limiter);
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(cors());
if (config.USE_HSTS) {
app.use(helmet.hsts({
maxAge: config.HSTS_MAX_AGE,
preload: config.HSTS_PRELOAD,
includeSubDomains: config.HSTS_SUBDOMAINS,
}));
}
app.use(function(req, res, next) {
res.setHeader('Server', '');
res.setHeader('Via', '');
next();
});
app.use(addRequestId);
app.use(compression());
app.use(function(req, res, next) {
logger.info({ req: req, res: res });
next();
});
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
app.use('/static', express.static(path.join(__dirname, 'public')));
app.set('json spaces', 2);
/* ------------------------------------------------------------------ *
API ROUTES
* ------------------------------------------------------------------ */
const router = getApiRoutes(config, new express.Router());
app.use('/api', router);
/* ------------------------------------------------------------------ *
ERROR HANDLING
* ------------------------------------------------------------------ */
// catch 404 and forward to error handler
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
logger.error({req: req, res: res, err: err});
res.status(err.status || 500)
.json({
message: err.message,
error: app.get('env') === 'development' ? err : {}
});
});
/* ------------------------------------------------------------------ *
SERVER
* ------------------------------------------------------------------ */
// start the server, but only if we're not in the middle of a test
if(!module.parent) {
app.listen(config.PORT, () => logger.info(`Started API server at http://0.0.0.0:${config.PORT}/`));
}
module.exports = app;