简体   繁体   English

如何使用自定义后端API将'create-react-app'部署到Heroku

[英]How to deploy 'create-react-app' to Heroku with custom back-end api

I'm trying to deploy a 'create-react-app' project to Heroku. 我正在尝试将“ create-react-app”项目部署到Heroku。 I used the Heroku Create-React-App boilerplate and followed another tutorial on setting up a backend api for connecting to my custom server.js file. 我使用了Heroku Create-React-App样板,并遵循了另一个教程来设置后端API以连接到我的自定义server.js文件。

When I deploy to Heroku, the build runs fine but my app still will not connect to my server.js file. 当我部署到Heroku时,构建可以正常运行,但是我的应用仍然无法连接到server.js文件。

I assume this is either because the proxy property in my package.json is supposed to point to the same port defined in my server.js file (which Heroku would assign automatically, which I don't know how to check) or perhaps my server.js file isn't running at all. 我认为这是因为package.jsonproxy属性应该指向我的server.js文件中定义的相同端口(Heroku会自动分配该端口,我不知道如何检查)或我的server.js文件根本没有运行。

Been having a very hard time finding any resources that properly explain how to deploy a 'create-react-app' with a custom back-end (server.js). 很难找到能够正确解释如何使用自定义后端(server.js)部署“ create-react-app”的资源。

package.json: package.json:

{
    "name": "my-app",
    "version": "0.1.0",
    "private": true,
    "proxy": "https://localhost:5000",
    "dependencies": {
      "axios": "^0.17.1",
      "body-parser": "^1.18.2",
      "md5": "^2.2.1",
      "mongodb": "^3.0.2",
      "react": "^16.2.0",
      "react-dom": "^16.2.0",
      "react-scripts": "1.1.1",
      "react-router": "^4.2.0",
      "react-router-dom": "^4.2.2"
    },
    "scripts": {
      "start": "react-scripts start",
      "start:server":"node server.js",
      "build": "react-scripts build",
      "test": "react-scripts test --env=jsdom",
      "eject": "react-scripts eject"
    }
}

Line in server.js where I set the port: server.js中设置端口的行:

const port = process.env.PORT || 5000;

I was able to deploy my app to Heroku by following this tutorial from fullstackreact.com and studying the accompanying repo. 通过遵循来自fullstackreact.com的本教程并研究随附的回购协议,我能够将我的应用程序部署到Heroku。

Basically, I needed to bundle my client-side code and then make sure my server.js file was serving that bundle as a static file. 基本上,我需要捆绑我的客户端代码,然后确保我的server.js文件将其作为静态文件提供。 I'm sure there's other ways to go about this, like running concurrent scripts for the webpack-dev-server and the backend api (server.js), but this worked for me without much headaches. 我确信还有其他方法可以解决此问题,例如为webpack-dev-server和后端api(server.js)运行并发脚本,但这对我来说没有太大的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM