forked from gothinkster/realworld-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_server.js
81 lines (66 loc) · 2.31 KB
/
dev_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
75
76
77
78
79
80
81
const path = require('path');
const express = require('express');
const cookieParser = require('cookie-parser');
const cookieEncrypter = require('cookie-encrypter');
const bodyParser = require('body-parser');
const webpack = require('webpack');
const webpackClientDevMiddleware = require('webpack-dev-middleware');
const webpackClientHotMiddleware = require('webpack-hot-middleware');
const webpackClientConfig = require('./webpack/config.client');
const serverConfig = require('./webpack/config.server');
const api = require('./src/api/routes');
const serverCompiler = webpack(serverConfig);
const clientCompiler = webpack(webpackClientConfig);
const port = Number(process.env.PORT) || 3000;
const app = express();
const nodeEnv = process.env.NODE_ENV || 'development';
const serverPath = path.resolve(__dirname, './dist/render.bundle.js');
let render = require(serverPath).default; // eslint-disable-line import/no-dynamic-require
app.set('env', nodeEnv);
app.use(cookieParser('change secret value'));
app.use(cookieEncrypter('change secret value'));
app.use(bodyParser.json());
app.use('/api', api);
app.use(webpackClientDevMiddleware(clientCompiler, {
publicPath: webpackClientConfig.output.publicPath,
headers: { 'Access-Control-Allow-Origin': '*' },
stats: { colors: true },
historyApiFallback: true,
}));
app.use(webpackClientHotMiddleware(clientCompiler, {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10000,
}));
app.use('/api', api);
app.use('/', (req, res, next) => render(req, res, next));
app.listen(port, () => {
console.log(`Listening at ${port}`);
});
function clearCache() {
const cacheIds = Object.keys(require.cache);
cacheIds.forEach((id) => {
if (id === serverPath) {
delete require.cache[id];
}
});
}
function onServerChange(err, stats) {
if (err || (stats.compilation && stats.compilation.errors && stats.compilation.errors.length)) {
console.log('Server bundling error:', err || stats.compilation.errors);
}
clearCache();
try {
render = require(serverPath).default; // eslint-disable-line import/no-dynamic-require, global-require, max-len
} catch (ex) {
console.log('Error detecded', ex);
}
}
function watch() {
const compilerOptions = {
aggregateTimeout: 300,
poll: 150,
};
serverCompiler.watch(compilerOptions, onServerChange);
}
watch();