gpt4 book ai didi

reactjs - React Routing with Express, Webpack 开发中间件, React router dom

转载 作者:行者123 更新时间:2023-12-04 14:58:00 25 4
gpt4 key购买 nike

我在使用 react-router-dom 和使用 express、webpack-dev-middleware、webpack-hot-middleware 的自定义开发服务器进行路由时遇到问题。

这是我的 webpack 配置。

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require('webpack');

module.exports = {
entry: [
// 'react-hot-loader/patch',
'webpack-hot-middleware/client?reload=true',
'./index.js'
],
output: {
filename: '[name].js',
path: path.join(__dirname, './dist'),
publicPath: '/',
clean: true,
chunkFilename: '[name].chunk.js',
},
mode: 'development',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.s[ac]ss$/,
exclude: /node_modules/,
use: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.(png|jpe?g|gif)$/,
type: 'asset/resource'
},
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
template: path.join(__dirname, "public", "index.html"),
title: 'Title'
}),
],
optimization: {
splitChunks: {
chunks: 'all',
},
},
}

这是我自定义的 dev express server.js 文件

const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const path = require('path');
const fs = require('fs');
const app = express();
const config = require("./webpack.dev.config");

const compiler = webpack(config);

//Enable "webpack-dev-middleware"
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
stats: {
chunks: false,
hash: false,
modules: false,
version: false,
assets: false,
entrypoints: false,
builtAt: false,
}
}));

//Enable "webpack-hot-middleware"
app.use(webpackHotMiddleware(compiler));

app.use(express.static('./public'));


// serve the routes
app.get('*', (req, res) => {
fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
if (err) {
res.sendStatus(404);
} else {
res.send(file.toString());
}
});
});

app.listen(8000, () => console.log('server listening on port 8000\n'))

当我打开应用程序时,所有路由都显示 404 not found。但是当我将 writeToDisk 选项添加到编译器选项时,一切正常。我希望路由在不写入磁盘的情况下工作。任何帮助表示赞赏。提前致谢!!

这是我的文件夹结构

Folder Structure

最佳答案

Github 问题页面上发布了此问题的解决方案 here .

为了使用 webpack-middleware 从内存中提供文件,而不是将它们写入磁盘,同时还能够明确地引用文件(例如在 res.render),建议的解决方案是在historyApiFallback前后添加两次webpackMiddleware,如下所示:

var webpackMiddleware = require("webpack-dev-middleware");
var historyApiFallback = require("connect-history-api-fallback");
var instance = webpackMiddleware(...);
app.use(instance);
app.use(historyApiFallback());
app.use(instance);

关于reactjs - React Routing with Express, Webpack 开发中间件, React router dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67580364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com