-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (99 loc) · 3.07 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
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
'use strict';
const bodyParser = require('body-parser');
const express = require('express');
const fetch = require('isomorphic-fetch');
const nunjucks = require('nunjucks');
const raven = require('raven');
const sentry = require('./lib/sentry');
const app = module.exports = express();
const router = new express.Router();
const API_HOSTNAME = process.env.CRM_API_URL;
app.set('json spaces', 2);
app.set('x-powered-by', false);
app.set('etag', 'strong');
app.use(bodyParser.json());
app.use('/assets', express.static(`${__dirname}/assets`));
// Configure nunjucks template engine
module.exports.nunjucks = nunjucks.configure('templates', {
autoescape: true,
express: app,
noCache: process.env.NODE_ENV !== 'production',
});
router.get('/', (req, res, next) => {
res.render('app.html', {
assets: process.env.NODE_ENV === 'production' ? '/assets' : 'http://frivillig-dev.app.dnt.local/assets',
environment: process.env.NODE_ENV,
});
});
router.post('/api/incident', (req, res, next) => {
const url = `${API_HOSTNAME}/api/Incident`;
const options = {
method: 'POST',
timeout: Number(process.env.CRM_API_TIMEOUT),
headers: {'Content-type': 'application/json'},
body: JSON.stringify(req.body),
};
fetch(url, options)
.then(response => {
res.status(response.status).send();
})
.catch(err => {
res.status(err.status || 500).json({err: err});
});
});
router.get('/api/activity', (req, res, next) => {
const url = `${API_HOSTNAME}/api/Activity`;
const options = {
timeout: Number(process.env.CRM_API_TIMEOUT),
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
};
let statusCode;
fetch(url, options)
.then(response => {
statusCode = response.status;
return response.json();
})
.then(json => {
res.status(statusCode).json(json);
})
.catch(err => {
res.json({err: err});
});
});
// Sentry Error Handling
if (process.env.NODE_ENV === 'production') {
router.use(raven.middleware.express.requestHandler(sentry));
router.use(raven.middleware.express.errorHandler(sentry));
}
// Final Error Handling
router.use((err, req, res, next) => {
/* 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 */
if (err.code && (typeof err.toJSON === 'function')) {
res.status(err.code).json(err.toJSON());
} else if (err.status && err.message) {
// Some errors, like SyntaxError from body-parser middleware
// https://github.com/expressjs/body-parser/issues/122
res.status(err.status).json({code: err.status, message: err.message});
} else {
res.status(500).json({code: 500, message: 'Unknown error'});
}
});
app.use(process.env.VIRTUAL_PATH, router);
if (!module.parent) {
const port = process.env.VIRTUAL_PORT || 8080;
app.listen(port);
console.log(`Server listening on port ${port}`); // eslint-disable-line no-console
}