我遇到了一个问题,我已经看到了很多问题,但我找不到适合我的解决方案。
只有当我推送到Heroku并从网上加载时才会出现此问题。如果我转到我网站的根目录(即domain.com),页面会加载。如果我点击此处,页面会在更改URL时使用正确的组件重新呈现。如果我刷新页面,键入URL,或通过任何方式导航到页面,但是在主页上发起的一组链接我得到了一个"内部服务器错误"。
我知道这是一个React-Router问题。我已经尝试将我的devServer配置historyApiFallback更改为true。我也尝试将historyApiFallback中的索引更改为我的应用程序的根目录。
更新 当我进入Heroku上的日志时,在尝试导航到domain.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: './'
}
};
答案 0 :(得分:3)
我想我已经解决了这个问题。对于server.js,我添加了
var path = require('path');
它开始正确路由。