forked from react-love/react-latest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (43 loc) · 1.4 KB
/
server.js
File metadata and controls
55 lines (43 loc) · 1.4 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
51
52
53
54
55
var express = require('express');
var cors = require('cors')
var webpack = require('webpack');
var webpackConfig = require('./dev.config.js');
var webpackDevMiddleware = require("webpack-dev-middleware");
var webpackHotMiddleware = require('webpack-hot-middleware');
var app = express();
var path = require('path')
var port = 3007;
var open = require('open')
var navigation = require(`./data/navigation.json`)
const compiler = webpack(webpackConfig);
app.use(require('morgan')('short'));
//跨域解决方案
app.use(cors())
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
headers: { "X-Custom-Header": "yes" },
stats: {
colors: true
}
}));
app.use(webpackHotMiddleware(compiler));
app.use(express.static(__dirname + '/build/'))
app.get('/book/navigation', function (req, res) {
res.json(navigation)
})
//请在这里添加你自己的测试接口
/*
* 注意,该方法必须 放在get和post等请求等最后,因为该方法是用来处理刷新浏览器找不到路径的问题。
* 如果你个人的get方法写到了该方法的下面,那么就无法执行你的方法。
* */
app.get('*', function(req, res) {
res.sendFile(path.join( __dirname, './index.html'));
});
app.listen(port, function(err){
if (err) {
console.log(err)
} else {
open(`http://localhost:${port}`)
}
})