-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·50 lines (42 loc) · 1.74 KB
/
Copy pathserver.js
File metadata and controls
executable file
·50 lines (42 loc) · 1.74 KB
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
import path from 'path';
import express from 'express';
import webpack from 'webpack';
const app = express();
const isProd = process.env.NODE_ENV === 'production';
const port = process.env.NODE_PORT || 3000;
const start = async () => {
if (!isProd) {
app.use((req, res, next) => {
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.set('Pragma', 'no-cache');
res.set('Expires', '0');
next();
});
const { default: webpackDevMiddleware } = await import('webpack-dev-middleware');
// const { default: webpackHotMiddleware } = await import('webpack-hot-middleware');
const { default: config } = await import('./webpack.dev.js');
const compiler = webpack(config);
const devMiddleware = webpackDevMiddleware(compiler, {
publicPath: '/built/',
});
app.use(devMiddleware);
app.get(['/', '/index.html'], (req, res, next) => {
devMiddleware.waitUntilValid(() => {
const indexPath = path.join(config.output.path, '../index.html');
devMiddleware.context.outputFileSystem.readFile(indexPath, (error, file) => {
if (error) {
next(error);
return;
}
res.set('Content-Type', 'text/html');
res.send(file);
});
});
});
// NOTE: Only the client bundle needs to be passed to `webpack-hot-middleware`.
// app.use(webpackHotMiddleware(compiler));
}
app.use('/', express.static('www'));
app.listen(port, () => console.log(`=== Go to http://localhost:${port} ===`));
};
void start();