我正在为dev使用一个快速服务器。我使用的是webpack -dev-来配置webpack。我想用express实现historyApiFallback的等价物。
服务器端支持webpack-dev- historyApiFallback。无论何时出现404错误,它都会忽略发送404,并让客户端通过历史api处理路由。
我怎么才能让它与express和webpack开发的中间件一起工作呢?
const webpackMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const webpackConfig = require('../webpack.config.js');
app.use(webpackMiddleware(webpack(webpackConfig), { publicPath: '/' }));发布于 2020-01-19 03:05:26
@MrBar评论是这个问题的正确答案。
下面是我如何让Express在任何404错误中为index.html提供服务:
const webpackConfig = require("./config/webpack.dev.js")
const compiler = webpack(webpackConfig)
const wdm = webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
})
// MrBar answer.
app.use((req, res, next) => {
if (!/(\.(?!html)\w+$|__webpack.*)/.test(req.url)) {
req.url = '/' // this would make express-js serve index.html
}
next()
})
// the next middleware is webpack-dev-middleware
app.use(wdm)
app.use(require("webpack-hot-middleware")(compiler, {
log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000
}))https://stackoverflow.com/questions/47696393
复制相似问题