我成立了一個代理來實現這一目標:
你有一個正規快件網絡服務器,供應中的index.html上的任何路線,除非其資產路線。如果它是一項資產,請求會被代理到web-dev-server上,您的反應仍然會直接指向webpack dev服務器,因此熱重新加載仍然有效。
讓我們假設你運行在8081的WebPack-DEV-服務器和您在8080你server.js文件的代理看起來就像這樣:
"use strict";
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./make-webpack-config')('dev');
var express = require('express');
var proxy = require('proxy-middleware');
var url = require('url');
## --------your proxy----------------------
var app = express();
## proxy the request for static assets
app.use('/assets', proxy(url.parse('http://localhost:8081/assets')));
app.get('/*', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
# -----your-webpack-dev-server------------------
var server = new WebpackDevServer(webpack(config), {
contentBase: __dirname,
hot: true,
quiet: false,
noInfo: false,
publicPath: "/assets/",
stats: { colors: true }
});
## run the two servers
server.listen(8081, "localhost", function() {});
app.listen(8080);
現在使你的入口點在的WebPack配置,如下所示:
entry: [
'./src/main.js',
'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:8081'
]
記爲hotreload
也是8081直接調用確保你傳遞一個絕對URL到output.publicPath
選項:
output: {
publicPath: "http://localhost:8081/assets/",
// ...
}
我的東西在這裏一個非常哈克版本,但它是脆弱的,只允許簡單的途徑來匹配:https://github.com/natew/react-base(請參閱使-webpack-config)和(app/routes.js) – 2014-10-05 14:50:10
你是否設法解決這個問題Nathan?如果是這樣如何?請嘗試在這裏回答我的問題http://stackoverflow.com/questions/31091702/why-do-we-need-a-proxy-on-an-express-js-server-in-order-to-get-webpack -hot-reloa。謝謝..! – SudoPlz 2015-06-27 18:08:11