forked from Choices-js/Choices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (59 loc) · 1.77 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
65
66
67
68
69
70
71
72
73
74
/* eslint-disable no-console,global-require,import/no-extraneous-dependencies */
const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 3001;
const DIST_DIR = path.resolve(__dirname, 'public');
const app = express();
if (process.env.NODE_ENV !== 'production') {
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config.dev');
console.log('Compiling bundle... 👷🏽');
const compiler = webpack(config);
app.use(
webpackDevMiddleware(compiler, {
publicPath: '/assets/scripts/',
stats: {
colors: true,
},
}),
);
app.use(webpackHotMiddleware(compiler));
app.get('/data', (req, res) => {
// prevent endpoint from being cached
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
const fakeData = [...new Array(50)].map((_, index) => ({
label: `Label ${index + 1}`,
value: `Value ${index + 1}`,
}));
setTimeout(() => {
res.status(200).send(fakeData);
}, 1000);
});
}
app.use(express.static(DIST_DIR));
const server = app.listen(PORT, err => {
if (err) {
console.log(err);
}
console.log(`Listening at http://localhost:${server.address().port} 👂`);
});
process.on('SIGTERM', () => {
console.log('Shutting down server');
if (server) {
server.close(err => {
if (err) {
console.log('Failed to shut down server');
process.exit(1);
}
console.log('Shut down server');
process.exit(0);
});
} else {
process.exit(0);
}
});
module.exports = server;