【问题标题】:I could not deploy my reactJS app with json-server我无法使用 json-server 部署我的 reactJS 应用程序
【发布时间】:2021-11-21 06:02:01
【问题描述】:

我有一个将 json-server 作为数据库的应用程序。我想在heroku中部署它。所以我创建了这样的服务器。

const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults({ static: './build' });
const port = process.env.PORT || 8000;
const cors = require('cors');
const corsOptions = {
  origin: '*',
  credentials: true, //access-control-allow-credentials:true
  optionSuccessStatus: 200,
};

server.use(cors(corsOptions));

server.use(middlewares);
server.use(router);

server.listen(port);

我还更改了包 json 文件脚本部分。

  "scripts": {
    "start": "concurrently \"react-scripts start\" \"json-server --watch db.json --port 8000 \"",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:8000",

我还创建了 Procfile 并添加了 网络:节点服务器.js

它在本地主机上工作。但是当我在 heroku 中部署它时,它并没有获取数据。 这也是无效的网络获取请求。

Request URL: http://localhost:8000/cars
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: https://car-app1.herokuapp.com
Cache-Control: no-cache
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Tue, 28 Sep 2021 07:51:14 GMT
ETag: W/"b63-2tPS49o89NSMpXwMALi1soZWiMs"
Expires: -1
Pragma: no-cache
Vary: Origin, Accept-Encoding
X-Content-Type-Options: nosniff
X-Powered-By: Express

【问题讨论】:

    标签: node.js reactjs mongodb heroku redux


    【解决方案1】:

    您的应用正在尝试访问本地主机。您应该使用 heroku 更改请求 url。

    错误的请求: http://localhost:8000/cars

    正确的请求: https://car-app1.herokuapp.com/cars/

    【讨论】:

      猜你喜欢
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 2018-06-12
      相关资源
      最近更新 更多