简体   繁体   English

找不到Webpack-dev-server子页面

[英]Webpack-dev-server sub pages not found

I'm using react with webpack. 我正在使用webpack反应。 My app worked well before using webpack-dev-server. 在使用webpack-dev-server之前,我的应用程序运行良好。 Only the home page works now, the others return "404 not found". 现在只有主页可以使用,其他则返回“ 404 not found”。 Maybe there's a problem in webpack.config.js ? 也许webpack.config.js中有问题?

var webpack = require("webpack");
var path = require("path");

var BUILD_DIR = path.resolve(__dirname, "public");
var APP_DIR = path.resolve(__dirname, "app");

var config = {
    entry: [
        "webpack/hot/only-dev-server",
        APP_DIR + "/main.js",
        "webpack-dev-server/client?localhost:8080"
    ],

    output: {
        path: BUILD_DIR + "/dist",
        filename: "bundle.js",
    },

    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        }),
        new webpack.HotModuleReplacementPlugin()
    ],

    module: {
        loaders: [...],
    },

    devServer: {
        noInfo: true,
        hot: true,
        inline: true
    },
}

module.exports = config;

And my script in package.json: 而我的脚本在package.json中:

"start": "webpack --watch & webpack-dev-server --content-base public"

Webpack generates well bundle file. Webpack生成良好的捆绑文件。 So i think the webpack-dev-server is the one who causes this problem. 所以我认为webpack-dev-server是导致此问题的人。

Thank you ! 谢谢 !

I've found the answer. 我找到了答案。 For someone who has the same problem, we need to add "historyApiFallback: true" to devServerConfig: 对于有相同问题的人,我们需要在devServerConfig中添加“ historyApiFallback:true”:

devServer: {
    noInfo: true,
    hot: true,
    inline: true,
    historyApiFallback: true
},

can you change your npm script 你能改变你的npm脚本吗

"start": "webpack --watch & webpack-dev-server --content-base public"

to

"start": "webpack --watch & webpack-dev-server --content-base public/"

and see what happens? 看看会发生什么? Otherwise I will comeback and assist you. 否则,我会卷土重来并为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM