-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (64 loc) · 2.14 KB
/
index.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
'use strict';
/* istanbul ignore if */
if (process.env.NODE_ENV === 'production') {
/* eslint-disable no-console */
console.log('Starting newrelic application monitoring');
/* eslint-enable */
require('newrelic'); // eslint-disable-line global-require
}
const raven = require('raven');
const sentry = require('./lib/sentry');
const express = require('express');
const compression = require('compression');
const responseTime = require('response-time');
const HttpError = require('@starefossen/http-error');
const app = module.exports = express();
const pg = require('./lib/pg');
app.set('json spaces', 2);
app.set('x-powered-by', false);
app.set('etag', false);
app.use(compression());
app.use(responseTime());
const VIRTUAL_PATH = process.env.VIRTUAL_PATH || '';
// Cors Headers
const corsHeaders = require('@starefossen/express-cors');
app.use(corsHeaders.middleware);
// Health Check
app.get(`${VIRTUAL_PATH}/CloudHealthCheck`, require('./controllers/health'));
// Routing API
app.use(VIRTUAL_PATH, require('./controllers/api'));
// Not Found
app.use((req, res, next) => next(new HttpError('Not Found', 404)));
// Sentry error handler
app.use(raven.middleware.express.requestHandler(sentry));
app.use(raven.middleware.express.errorHandler(sentry));
// Final error handler
app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
// Wrap non-http errors
if (!(err instanceof HttpError)) {
err = new HttpError(err.message, 500, err); // eslint-disable-line no-param-reassign
}
/* eslint-disable no-console */
if (err.code >= 500) {
if (err.error) {
console.error(err.error.message);
console.error(err.error.stack);
} else {
console.error(err.message);
console.error(err.stack);
}
}
/* eslint-enable */
res.status(err.code).json(err.toJSON());
});
/* istanbul ignore if */
if (!module.parent) {
pg.connect(err => {
if (err) { throw err; }
/* eslint-disable no-console */
console.log('Connected to Postgres Database');
app.listen(process.env.VIRTUAL_PORT || 8080);
console.log(`Server listening on port ${process.env.VIRTUAL_PORT || 8080}`);
/* eslint-enable */
});
}