从理论上讲,这应该可以工作,在/
文件夹中添加文件/static.json
,使所有内容都进入React index.html
。
(相关)
React Routing works in local machine but not Heroku
https://github.com/heroku/heroku-buildpack-static#configuration
就我而言,它不起作用,我得到了Cannot GET /hello
而不是收到index.html。出乎意料的是,如果我直接进入/index.html
,则会看到该页面,但没有激活任何路由。
发生了什么事,我该如何解决?
更新:尝试没有运气
/static.jason
/src/static.jason
/client/static.jason
/client/src/static.jason
/static.json
{
"routes": {
"/**": "index.html"
}
}
/client/src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import FileUpload from './FileUpload';
ReactDOM.render(
<div><FileUpload /></div>,
document.getElementById('root')
);
/client/src/FileUpload.mjs
import React, { Component } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
class TextFileReader extends Component {
render() {
return (
<Router>
<div>
<Route path="/heyyo" render={() => <span>heyyo</span>} />
<Route exact path="/" render={() => <div>resting</div>} />
<Route path="/what" render={() => <div>what</div>} />
NO ROUTES HERE
</div>
</Router>
)
}
}
export default TextFileReader;
答案 0 :(得分:0)
最后,请重新阅读此书React Routing works in local machine but not Heroku,我已将此行添加到我的Express
app.get('/*', (req, res) => {
let url = path.join(__dirname, '../client/build', 'index.html');
if (!url.startsWith('/app/')) // we're on local windows
url = url.substring(1);
res.sendFile(url);
});
并且一旦部署到Heroku,它就会起作用:)