-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (56 loc) · 2.09 KB
/
server.js
File metadata and controls
69 lines (56 loc) · 2.09 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var path = require('path')
var webpack = require('webpack')
var webpackDevServer = require('webpack-dev-server')
var config = require('./webpack.dev.config')
var opn = require('opn')
var ip = '0.0.0.0'
var port = 9000
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var proxy = require('express-http-proxy')
var webpackDevServerEntries = ['react-hot-loader/patch', 'webpack-dev-server/client?http://' + ip + ':' + port, 'webpack/hot/only-dev-server']
if (typeof config.entry === 'string') {
config.entry = webpackDevServerEntries.concat([config.entry])
}else if(Array.isArray(config.entry)){
config.entry = webpackDevServerEntries.concat(config.entry)
}else if(config.entry){
for(var k in config.entry){
var main = config.entry[k]
config.entry[k] = webpackDevServerEntries.concat(Array.isArray(main) ? main : [main])
}
}
var options = {
contentBase: path.resolve(__dirname, './'),
hot: true,
disableHostCheck : true,
//设置webpack-dev-server启动的时候,bundles的输出的路径,打包的时候这个publicPath没有作用
publicPath: config.output.publicPath,
historyApiFallback: false,
// /api/* 会指向 http://127.0.0.1:9001/api/* 如 /api/users 就会指向 http://127.0.0.1:9001/api/users
// proxy: {
// '/weixinapi/*': {
// target: 'http://127.0.0.1:9001',
// }
// }
}
var compiler = webpack(config)
var server = new webpackDevServer(compiler, options)
server.use('/api', proxy('http://127.0.0.1:9001', {
forwardPath: function (req, res) {
var redirect = require('url').parse(req.url).path
return '/api' + redirect
},
https: false,
reqBodyEncoding: null
}))
server.use(bodyParser.json({limit : '1000000kb'})) // for parsing application/json
server.use(bodyParser.urlencoded({ extended: true ,limit : '1000000kb'})) // for parsing application/x-www-form-urlencoded
server.use(cookieParser())
server.listen(port,ip, function (err) {
if (err) {
console.log(err) //eslint-disable-line no-console
} else {
opn('http://127.0.0.1:' + port)
console.log('Listening at http://127.0.0.1:' + port) //eslint-disable-line no-console
}
})