-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic-server.js
38 lines (29 loc) · 965 Bytes
/
static-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
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./config/server-config');
const webpackConfig = require('./webpack.config');
const app = express();
if (!config.DEBUG) {
throw new Error('Static server should not be run in production');
}
const compiler = webpack(webpackConfig);
const indexFilename = path.join(compiler.outputPath, 'index.html');
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
}));
app.use(webpackHotMiddleware(compiler));
app.use((req, res, next) => {
compiler.outputFileSystem.readFile(indexFilename, (err, result) => {
if (err) {
next(err);
} else {
res.set('content-type', 'text/html');
res.send(result);
}
});
});
module.exports = app;