0
我是新來的反應。我創建了使用兩個服務器,它擁有我所有的API的路由的prod.server.js
和dev.server.js
它初始化的WebPack-DEV-服務器,並從prod.server.js
運行一個單獨的端口和代理的所有請求/api/*
路徑到生產服務器的應用程序。我分離了這兩個,所以當我最終將我的應用程序推向生產時,我不需要編輯服務器。ReactJS:Hot Reload不能正常工作
這裏是我的兩個服務器:
生產服務器(prod.server.js)
const express = require("express");
const api = require('./server/routes/api')
const http = require('./server/routes/http')
const app = express();
app.use(express.static(process.cwd() + '/public'));
api(app, __dirname + "/public");
http(app, __dirname + "/public");
app.get('/*', (req, res) => {
res.sendFile(__dirname + "/index.html")
});
app.listen(process.env.NODE_ENV || 8001, function() {
console.log("Application started on port", process.env.NODE_ENV || 8001);
});
開發服務器(dev.server.js)
var WebpackDevServer = require('webpack-dev-server');
const api = require('./server/routes/api')
const http = require('./server/routes/http')
var webpack = require('webpack');
var config = require('./webpack.config');
var path = require('path');
var compiler = webpack(config);
var server = new WebpackDevServer(compiler, {
proxy: {
'/api': {
target: 'http://localhost:8001',
secure: false
}
},
historyApiFallback: true,
hot: true,
filename: 'bundle.js',
publicPath: '/',
stats: {
colors: true,
},
});
server.listen(8000, function() {
console.log("Application Dev running at port 8000");
});
我期待當我更新我的反應時,瀏覽器應該選擇相應的更改並重新加載,但這不會發生。我必須重新加載,而且我一直這樣做了一段時間,但是現在我已經變得狡猾了,我寧願有熱的重新加載,任何幫助?
如果需要,下面是我的WebPack配置。
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: [
__dirname + "/client/index.js"
],
output: {
path: __dirname + "/public",
filename: "bundle.js"
},
module: {
loaders: [
{
test: /\.js$/,
loader: ['babel'],
query:
{
presets:
[
'es2015',
'react'
]
},
exclude: /node_modules/
},
{
test: /\.css$/,
loaders: ['style', 'css'],
exclude: /node_modules/
}
]
},
plugins:[
new HtmlWebpackPlugin({
template: __dirname + '/client/index.html'
})
]
}