This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-test.js
73 lines (62 loc) · 2.24 KB
/
server-test.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
const path = require('path');
const process = require('process');
const express = require('express');
const mustacheExpress = require('mustache-express');
const Promise = require('promise');
const compression = require('compression');
const helmet = require('helmet');
const createEnvSettingsFile = require('./src/build/scripts/envSettings');
createEnvSettingsFile(path.resolve(`${__dirname}/dist/js/settings.js`));
const server = express();
server.use(helmet());
server.use(compression());
server.set('views', `${__dirname}/dist`);
server.set('view engine', 'mustache');
server.engine('html', mustacheExpress());
console.error('process.env.PORT', process.env.PORT);
console.error('process.env.PUBLIC_PATH', process.env.PUBLIC_PATH);
console.error('process.env.API_URL', process.env.API_URL);
const PUBLIC_PATH = process.env.PUBLIC_PATH;
server.use(`/dist`, express.static(path.resolve(__dirname, 'dist')));
server.use(`${PUBLIC_PATH}/dist/js`, express.static(path.resolve(__dirname, 'dist/js')));
server.use(`${PUBLIC_PATH}/dist/css`, express.static(path.resolve(__dirname, 'dist/css')));
const routerHealth = express.Router();
server.use(`${PUBLIC_PATH}/health`, routerHealth);
routerHealth.get('/isAlive', (req, res) => res.sendStatus(200));
routerHealth.get('/isReady', (req, res) => res.sendStatus(200));
const renderApp = () =>
new Promise((resolve, reject) => {
server.render('index.html', (err, html) => {
if (err) {
reject(err);
} else {
resolve(html);
}
});
});
const startServer = (html) => {
server.get(/^\/(?!.*dist).*$/, (req, res) => {
res.send(html);
});
const port = process.env.PORT || 8084;
server.listen(port, () => {
console.log(`Server-test Web App listening on port: ${port}`);
});
};
const startExpressWebServer = async () => {
if (!process.env.PUBLIC_PATH) {
console.error('PUBLIC_PATH env var must be defined!');
process.exit(1);
}
if (!process.env.API_URL) {
console.error('API_URL env var must be defined!');
process.exit(1);
}
try {
const html = await renderApp();
startServer(html);
} catch (e) {
console.error(e);
}
};
startExpressWebServer();