-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
43 lines (35 loc) · 1.37 KB
/
app.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
const path = require('path');
const express = require('express');
const app = express();
const port = (process.env.PORT || 8080);
const indexPath = path.join(__dirname, './index.html');
const publicPath = express.static(path.join(__dirname, './'));
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.dev.config.js');
const compiler = webpack(config);
app.use(webpackHotMiddleware(compiler));
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
before: (app) => {
app.use('/node_modules/**', (req, res) => {
const modulesPath = req.baseUrl.replace('/', '');
const stream = fs.createReadStream(path.resolve(__dirname, '..', modulesPath));
res.writeHead(200, {'Content-Type': mimeTypes.lookup(modulesPath)});
stream.pipe(res);
});
}
}));
app.use('/', publicPath);
app.get('/', function(_, res) {
res.sendFile(indexPath);
});
} else {
app.use('/', publicPath);
app.get('*', (req, res) => res.sendFile(indexPath));
}
app.listen(port);
console.log(`Listening at http://localhost:${port}`);