-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (45 loc) · 1.34 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
/* eslint-disable no-console */
const last = require('lodash').last;
const open = require('open');
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const morgan = require('morgan');
const proxy = require('proxy-middleware');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack.config');
const app = express();
const server = new WebpackDevServer(webpack(config), config.devServer);
server.listen(4000, 'localhost', (err) => {
if (err) {
console.log(err);
} else {
console.log(`
=====================================================
WebpackDevServer listen at http://localhost:${4000}
=====================================================
`);
}
});
app.use(morgan('dev'));
app.use('/js', proxy('http://localhost:4000/js'));
app.use((req, res) => {
const options = { root: path.join(__dirname, 'dist') };
const isJS = !!req.originalUrl.match(/\.js$/);
const filename = isJS ? last(req.originalUrl.split('/')) : 'index.html';
return res.sendFile(filename, options, (err) => {
if (err) {
res.status(404).send({
code: 404,
msg: 'All is not right in the world :(',
});
}
});
});
app.listen(3000, (err) => {
if (err) {
console.error(err);
} else {
open(`http://localhost:${3000}`);
}
});