-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
37 lines (28 loc) · 928 Bytes
/
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
const express = require('express');
const app = express();
const port = 3000;
const buildConfigData = () => {
const config = {}
const configEnvPrefix = 'SPA_';
for (const [key, value] of Object.entries(process.env))
if (key.startsWith(configEnvPrefix))
config[key.replace(configEnvPrefix, '')] = value;
return config;
}
const configData = buildConfigData();
const getConfigRoute = () => {
let route = process.env.CONFIG_ROUTE;
if (route)
route = route.replace(/[^a-zA-Z0-9-_]/g, '');
return route ? `/${route}` : '/config';
}
const configRoute = getConfigRoute();
app.use(express.static('public'));
app.get(configRoute, (req, res) => {
res.json(configData);
})
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
console.log(`Serving config on route '${configRoute}'`);
console.log('Config:', JSON.stringify(configData));
})