React Routing在本地机器上工作,但在Heroku上不工作。
React Routing在本地机器上工作,但在Heroku上不工作。
React/react router/heroku的问题(很可能是在heroku上出了问题)。
我正在按照这个很棒的教程进行操作:https://medium.com/@patriciolpezjuri/using-create-react-app-with-react-router-express-js-8fa658bf892d#.y77yjte2j
一切都进行得很顺利,直到我将它发布到heroku并尝试导航到https://appname.herokuapp.com/about,我收到了一个404 Not Found/nginx错误。当然,根据教程,应该显示一个关于页面。
React router在heroku上不起作用,我无法弄清楚为什么。
我尝试修改server/app.js
文件,如在这里建议的:React routes are not working in facebook's create-react-app build
// server/app.js const express = require('express'); const morgan = require('morgan'); const path = require('path'); const app = express(); console.log('hi from /src/server.js') // 设置日志记录 app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] :response-time ms')); // 提供静态资源 app.use(express.static(path.resolve(__dirname, '..', 'build'))); // 始终返回主要的index.html,这样react-router可以在客户端渲染路由 app.get('/about', (req, res) => { console.log('hi from app.get.about') console.log(req) console.log(res) res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html')); }); app.get('/*', (req, res) => { console.log('hi from app.get') console.log(req) console.log(res) res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html')); }); module.exports = app;
但是它不起作用,也没有在控制台中记录任何内容:
2017-01-20T21:03:47.438140+00:00 heroku[web.1]: Starting process with command `bin/boot` 2017-01-20T21:03:49.540005+00:00 app[web.1]: Injecting runtime env into /app/build/static/js/main.242e967b.js (from .profile.d/inject_react_app_env.sh) 2017-01-20T21:03:49.695317+00:00 app[web.1]: Starting log redirection... 2017-01-20T21:03:49.695899+00:00 app[web.1]: Starting nginx... 2017-01-20T21:03:51.108255+00:00 heroku[web.1]: State changed from starting to up 2017-01-20T21:04:22.720627+00:00 heroku[router]: at=info method=GET path="/" host=sentieoapp1.herokuapp.com request_id=fb8bc13b-f6b5-47bc-8330-443f28e211df fwd="132.147.73.97" dyno=web.1 connect=0ms service=3ms status=200 bytes=627 2017-01-20T21:04:22.746761+00:00 app[web.1]: 10.158.165.5 - - [20/Jan/2017:21:04:22 +0000] "GET / HTTP/1.1" 200 386 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" 2017-01-20T21:04:23.076521+00:00 app[web.1]: 10.158.165.5 - - [20/Jan/2017:21:04:23 +0000] "GET /static/js/main.242e967b.js HTTP/1.1" 200 62263 "https://sentieoapp1.herokuapp.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" 2017-01-20T21:04:23.056416+00:00 heroku[router]: at=info method=GET path="/static/js/main.242e967b.js" host=sentieoapp1.herokuapp.com request_id=436d5ce5-ee39-4ab7-9e12-f5871e0fd552 fwd="132.147.73.97" dyno=web.1 connect=0ms service=25ms status=200 bytes=62540 2017-01-20T21:04:23.745285+00:00 heroku[router]: at=info method=GET path="/static/css/main.9a0fe4f1.css" host=sentieoapp1.herokuapp.com request_id=80438aaa-58c4-456e-8df9-7a29e49bc4ba fwd="132.147.73.97" dyno=web.1 connect=0ms service=2ms status=200 bytes=560 2017-01-20T21:04:23.766676+00:00 app[web.1]: 10.158.165.5 - - [20/Jan/2017:21:04:23 +0000] "GET /static/css/main.9a0fe4f1.css HTTP/1.1" 200 301 "https://sentieoapp1.herokuapp.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" 2017-01-20T21:04:24.044940+00:00 heroku[router]: at=info method=GET path="/static/media/logo.5d5d9eef.svg" host=sentieoapp1.herokuapp.com request_id=bcbc1906-3b90-4f13-a700-f432f79c725d fwd="132.147.73.97" dyno=web.1 connect=0ms service=1ms status=200 bytes=2902 2017-01-20T21:04:24.065013+00:00 app[web.1]: 10.158.165.5 - - [20/Jan/2017:21:04:24 +0000] "GET /static/media/logo.5d5d9eef.svg HTTP/1.1" 200 2671 "https://sentieoapp1.herokuapp.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" 2017-01-20T21:04:26.264631+00:00 heroku[router]: at=info method=GET path="/about" host=sentieoapp1.herokuapp.com request_id=0caef324-9268-4ebb-a3f5-0fb047100893 fwd="132.147.73.97" dyno=web.1 connect=0ms service=4ms status=404 bytes=403 2017-01-20T21:04:26.284717+00:00 app[web.1]: 10.158.165.5 - - [20/Jan/2017:21:04:26 +0000] "GET /about HTTP/1.1" 404 191 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"
这就是我卡住的地方。我熟悉Express,并且之前在heroku上使其工作过,但这是一个完全不同级别的噩梦。我明白这不是服务器端路由,而是React在单个index.html页面中进行的路由。但是如果我可以在本地机器上让它工作,为什么在Heroku上不起作用?