Heroku无法获取/反应应用程序

时间:2017-11-09 09:55:27

标签: node.js reactjs heroku npm yarnpkg

我正在使用React& NodeJS和纱线包经理。我的应用在本地工作正常。但当我把它上传到Heroku时,它说无法获取/

  

无法加载资源:服务器响应状态为404(未找到)

我的webpack.config.prod.js文件看起来像这样

 const JS_BUNDLE   = 'bundle.js';

module.exports = function (paths, loaders, plugins) {
  return {
    output: {
      path: paths.dist,
      filename: JS_BUNDLE,
    },
    module: {
      rules: [
        loaders.style,
      ],
    },
    devtool: 'source-map',
    plugins: [
      plugins.loaderOptions({
        minimize: true,
      }),
      plugins.uglifyJs({
        output: {
          comments: false,
        },
        compress: {
          warnings: false,
          drop_console: true,
          dead_code: true,
          unused: true,
          conditionals: true,
                comparisons: true,
                sequences: true,
                evaluate: true,
                if_return: true,
                join_vars: true,
        },
      }),
      plugins.definePlugin,
      plugins.genHtml({
        filename: 'index.html',
        template: 'template.html',
      }),
    ],
  };
};

我相信它在package.json

上运行yarn和我的脚本时不会创建index.html
"scripts": {
"watch-dev": "NODE_ENV=development webpack-dev-server --hot --inline",
"api": "PORT=3000 nodemon server.js",
"start": "node server.js"

}

Heroku Logs

  

2017-11-09T09:31:46.907824 + 00:00 heroku [web.1]:状态由开始向上变为

     

2017-11-09T09:31:48.165220 + 00:00 app [web.1]:GET / 404 139 - 55.164 ms

     

2017-11-09T09:31:48.154963 + 00:00 heroku [router]:at = info method = GET path =" /" host = hksmetal.herokuapp.com request_id = 0692c07a-6f96-4708-8a74-30704fbad48e fwd =" 213.247.123.26" dyno = web.1 connect = 1ms service = 128ms status = 404 bytes = 543 protocol = https

如果有任何建议,请提前致谢

1 个答案:

答案 0 :(得分:0)

您需要在index.html中添加一个返回值。 app.use(express.static(path.join(__dirname, 'build')));

app.get('*', function(req, res) { res.sendFile(path.join(__dirname + '/build/index.html')); });