This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (52 loc) · 1.88 KB
/
server.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
/* eslint-disable */
const express = require('express');
const path = require('path');
const prometheus = require('prom-client');
const getHtmlWithDecorator = require('./server/getHtmlWithDecorator');
const winstonLogger = require('./server/winstonLogger');
const appProxy = require('./server/appProxy');
// Prometheus metrics
const collectDefaultMetrics = prometheus.collectDefaultMetrics;
collectDefaultMetrics({ timeout: 5000 });
const server = express();
const env = process.argv[2];
server.disable('x-powered-by');
const DIST_DIR = path.join(__dirname, './dist');
const HTML_FILE = path.join(DIST_DIR, 'index.html');
function disableCache(res) {
res.setHeader('Pragma', 'no-cache');
res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.setHeader('Expires', '-1');
}
server.get('/', (req, res) => {
res.redirect('/syk/oppfolgingsplan');
});
server.use(['/static', '/syk/oppfolgingsplan/static'], express.static(DIST_DIR, { index: false }));
server.get('/internal/isAlive|isReady', (req, res) => res.sendStatus(200));
if (env === 'opplaering') {
require('./mock/mockEndepunkter').mockForOpplaeringsmiljo(server);
} else if (env === 'local') {
require('./mock/mockEndepunkter').mockForOpplaeringsmiljo(server);
require('./mock/mockEndepunkter').mockForLokaltMiljo(server);
} else {
appProxy(server);
server.get('/actuator/metrics', (req, res) => {
res.set('Content-Type', prometheus.register.contentType);
res.end(prometheus.register.metrics());
});
}
server.use(express.json());
server.use('*', (req, res) =>
getHtmlWithDecorator(HTML_FILE)
.then((html) => {
disableCache(res);
res.send(html);
})
.catch((e) => {
winstonLogger.error(e);
disableCache(res);
res.status(500).send(e);
})
);
const port = process.env.PORT || 8080;
server.listen(port, () => winstonLogger.info(`App listening on port: ${port}`));