"Internal Server Error" 在刷新页面时或在使用 React Router 时手动输入 URL

"Internal Server Error" when refreshing page or typing URL manually when using React Router

我遇到了很多问题,但我找不到适合我的解决方案。

只有当我推送到 Heroku 并从网络加载它时才会出现此问题。如果我转到我网站的根目录(即 domain.com)页面负载。如果我四处点击,页面会在更改 URL 时使用正确的组件重新呈现。如果我刷新页面,键入 URL,或通过除了一组源自主页的链接以外的任何方式导航到该页面,我会得到一个 "Internal Server Error"。

我知道这是 React-Router 的问题。我尝试将我的 devServer 配置 historyApiFallback 更改为 true。我还尝试将 historyApiFallback 中的索引更改为我的应用程序的根目录。

更新 当我进入 Heroku 上的日志时,我在尝试导航到域时遇到此错误。com/activity

2017-11-13T05:43:36.957344+00:00 app[web.1]: ReferenceError: path is not defined
2017-11-13T05:43:36.957347+00:00 app[web.1]:     at /app/server.js:7:23
2017-11-13T05:43:36.957348+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-11-13T05:43:36.957349+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/route.js:137:13)
2017-11-13T05:43:36.957350+00:00 app[web.1]:     at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2017-11-13T05:43:36.957351+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-11-13T05:43:36.957352+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:281:22
2017-11-13T05:43:36.957353+00:00 app[web.1]:     at param (/app/node_modules/express/lib/router/index.js:354:14)
2017-11-13T05:43:36.957354+00:00 app[web.1]:     at param (/app/node_modules/express/lib/router/index.js:365:14)
2017-11-13T05:43:36.957355+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:410:3)
2017-11-13T05:43:36.957355+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:275:10)

package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "App",
  "main": "index.js",
  "repository": "xxx@github.com",
  "scripts": {
    "start": "webpack-dev-server --inline --content-base . --history-api-fallback",
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
    "test:watch": "npm run test -- --watch",
    "prod": "NODE_ENV=production node server.js",
    "postinstall": "webpack -p"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.2.1",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "chai": "^3.5.0",
    "chai-jquery": "^2.0.0",
    "jquery": "^2.2.1",
    "jsdom": "^8.1.0",
    "mocha": "^2.4.5",
    "react-addons-test-utils": "^0.14.7",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0"
  },
  "dependencies": {
    "axios": "^0.16.2",
    "babel-preset-stage-1": "^6.1.18",
    "lodash": "^3.10.1",
    "react": "^0.14.3",
    "react-autosuggest": "^9.3.2",
    "react-dom": "^0.14.3",
    "react-dropzone": "^4.2.1",
    "react-pdf": "^2.1.1",
    "react-redux": "4.3.0",
    "react-router": "^2.0.1",
    "react-router-dom": "^4.1.2",
    "react-select": "^1.0.0-rc.10",
    "recharts": "^1.0.0-beta.0",
    "redux": "^3.0.4",
    "redux-promise": "^0.5.3",
    "redux-thunk": "^2.2.0"
  }
}

server.js

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/'));
app.get('*', function (request, response){
    response.sendFile(path.resolve(__dirname, 'index.html'))
});
app.listen(process.env.PORT || 8080);

webpack.config.js

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

我想我已经解决了这个问题。对于 server.js,我添加了

var path = require('path');

它开始正确路由。