【问题标题】:static.json not working for ReactJS routes and Herokustatic.json 不适用于 ReactJS 路由和 Heroku
【发布时间】:2019-03-22 00:21:21
【问题描述】:

理论上这应该可行,在/ 文件夹中添加文件/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;

【问题讨论】:

  • 你把你的 static.json 放在哪里了?使用您的客户端或服务器 package.json?

标签: reactjs heroku react-router


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 2020-02-19
    • 2022-01-24
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多