我正在使用dev的快速服务器。我正在使用webpack-dev-middleware进行webpack配置。我想使用express实现相当于historyApiFallback。
historyApiFallback可用于webpack-dev-server。每当出现404错误时,它将忽略发送404并让客户端通过历史记录api处理路由。
如何使用express和webpack-dev-middleware?
const webpackMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const webpackConfig = require('../webpack.config.js');
app.use(webpackMiddleware(webpack(webpackConfig), { publicPath: '/' }));
答案 0 :(得分:0)
@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
}))