gpt4 book ai didi

reactjs - static.json 不适用于 ReactJS 路由和 Heroku

转载 作者:行者123 更新时间:2023-12-05 06:30:00 25 4
gpt4 key购买 nike

理论上这应该可行,在 / 文件夹中添加文件 /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,我会得到该页面,但没有激活任何路由。

发生了什么,我该如何解决?

UPDATE: tried without luck /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;

最佳答案

最后,重新阅读更好的 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,它就可以工作了:)

关于reactjs - static.json 不适用于 ReactJS 路由和 Heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52852877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com