Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为什么我只能在根(/)上点击我的react/webpack应用程序?_Reactjs_Webpack - Fatal编程技术网

Reactjs 为什么我只能在根(/)上点击我的react/webpack应用程序?

Reactjs 为什么我只能在根(/)上点击我的react/webpack应用程序?,reactjs,webpack,Reactjs,Webpack,今天我用热加载程序设置react transform。我有一个包含两条路线的应用程序,/和/reports。我可以点击/,然后点击链接将我带到/reports,效果很好,但如果我尝试直接转到/reports,我会得到一个404。这是我的webpack.config.js module.exports = { devtool: 'source-map', entry: { app: [ './App.js', 'web

今天我用
热加载程序设置
react transform
。我有一个包含两条路线的应用程序,
/
/reports
。我可以点击
/
,然后点击
链接
将我带到
/reports
,效果很好,但如果我尝试直接转到
/reports
,我会得到一个
404
。这是我的
webpack.config.js

module.exports = {
    devtool: 'source-map',
    entry: {
        app: [
            './App.js',
            'webpack-hot-middleware/client'
        ],
        vendors: vendors
    },
    output: {
        filename: './bundle.[hash].js',
        chunkFilename: './[id].[hash].js',
        path: __dirname + '/dist'
    },
    module: {
        loaders: [
            {
                test: /\.js?$/,
                exclude: /(node_modules)/,
                loader: 'babel',
                query: {
                    presets: ['react', 'es2015']
                }
            }, {
                test: /\.scss$/,
                loaders: ["style", "css", "sass"]
            },
            {
                test: /\.(png|jpg)$/,
                loader: 'url-loader?limit=8192'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.ejs'
        }),

        new CommonsPlugin({
            name: 'vendors',
            filename: 'vendors.js',
            minChunks: Infinity
        }),

        new CleanWebpackPlugin(['dist'], {
            root: __dirname,
            verbose: true,
            dry: false
        }),

        new webpack.optimize.OccurenceOrderPlugin(),

        new webpack.HotModuleReplacementPlugin(),

        new webpack.NoErrorsPlugin()
    ]
};
var app = express();

var compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler));
app.use(webpackHotMiddleware(compiler));
app.use(express.static('/'));

app.listen(3000);
这是我的
server.js

module.exports = {
    devtool: 'source-map',
    entry: {
        app: [
            './App.js',
            'webpack-hot-middleware/client'
        ],
        vendors: vendors
    },
    output: {
        filename: './bundle.[hash].js',
        chunkFilename: './[id].[hash].js',
        path: __dirname + '/dist'
    },
    module: {
        loaders: [
            {
                test: /\.js?$/,
                exclude: /(node_modules)/,
                loader: 'babel',
                query: {
                    presets: ['react', 'es2015']
                }
            }, {
                test: /\.scss$/,
                loaders: ["style", "css", "sass"]
            },
            {
                test: /\.(png|jpg)$/,
                loader: 'url-loader?limit=8192'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.ejs'
        }),

        new CommonsPlugin({
            name: 'vendors',
            filename: 'vendors.js',
            minChunks: Infinity
        }),

        new CleanWebpackPlugin(['dist'], {
            root: __dirname,
            verbose: true,
            dry: false
        }),

        new webpack.optimize.OccurenceOrderPlugin(),

        new webpack.HotModuleReplacementPlugin(),

        new webpack.NoErrorsPlugin()
    ]
};
var app = express();

var compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler));
app.use(webpackHotMiddleware(compiler));
app.use(express.static('/'));

app.listen(3000);
我已尝试使用添加路线

app.get('*', function(req, res) {
  res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
但是
/dist/index.html
不存在,因为
热加载程序在内存中有它。确切的错误是

Error: ENOENT: no such file or directory, stat '/Users/jason/Developer/stack/webapp/dist/index.html'
   at Error (native)

我需要在配置中更改什么?

我的
express
服务器设置错误。我将此路线添加到
express

app.get('*', function(req, res) {
    var memoryFs = compiler.outputFileSystem;
    var index = path.join(webpackConfig.output.path, 'index.html');
    var html = memoryFs.readFileSync(index);
    res.end(html)
});

现在我工作得很好。

404是从express还是前面配置的404路线?它来自
express
。我在上面添加了错误消息。