-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
55 lines (50 loc) · 1.44 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
const isProduction = process.env.NODE_ENV === 'production';
const path = require('path');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack.config');
const express = require('express');
const port = process.env.PORT || 9090;
let app;
const compiler = webpack(config);
if (isProduction) {
const gzipFiles = ['.js'];
app = express();
app.get(gzipFiles, (req, res) => {
res.set('Content-Encoding', 'gzip');
});
app.use('/assets', express.static(`${__dirname}/assets`));
app.get('*.js', (req, res) => {
res.sendFile(path.join(__dirname, `dist/${req.url}`));
});
app.get('*.css', (req, res) => {
res.sendFile(path.join(__dirname, `dist/${req.url}`));
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
} else {
app = express();
console.log('in dev start');
app.use(require('webpack-hot-middleware')(compiler));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'src/index.html'));
});
app = new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
hotOnly: true,
inline: true,
historyApiFallback: true,
compress: true,
open: true,
stats: 'minimal',
});
console.log('in dev last');
}
app.listen(port, (err) => {
if (err) {
return console.error(err);
}
return console.log(`Listening at http://localhost:${port}/`);
});