[英]How to deploy to Heroku with Nodejs frontend, backend, and Docker
There are similar posts, but my case is pretty specific on NodeJS-frontend-backend-Docker-Heroku, so I have stuck for a whole week now.有类似的帖子,但我的案例在 NodeJS-frontend-backend-Docker-Heroku 上非常具体,所以我已经坚持了整整一周。
Task: I have 2 folders frontend and backend.任务:我有 2 个文件夹前端和后端。 I have Dockerfile in both of them like this /frontend/Dockerfile and /backend/Dockerfille.
我在这两个文件中都有 Dockerfile,例如 /frontend/Dockerfile 和 /backend/Dockerfille。 I also have docker-compose.yml and Procfile on the root directory.
我在根目录中还有 docker-compose.yml 和 Procfile。
Attempt 1: change to Dockerfile.frontend and Dockerfile.backend.尝试 1:更改为 Dockerfile.frontend 和 Dockerfile.backend。 I use
heroku:container push --recursive
.我使用
heroku:container push --recursive
。 Successfuly deploy it on Heroku, I already have scale=1
too.在 Heroku 上成功部署,我也已经有
scale=1
。 It doesn't work.它不起作用。
Attempt 2: change Dockerfile.frontend to Dockerfile.web and Dockerfile.backend.尝试 2:将 Dockerfile.frontend 更改为 Dockerfile.web 和 Dockerfile.backend。 The frontend web is working, but I can't login so I can't say for sure.
前端 web 正在运行,但我无法登录,所以我不能确定。 I can't call api request to backend with
POST https://mycoolapp.herokuapp.com/api/users/login
.我无法使用
POST https://mycoolapp.herokuapp.com/api/users/login
向后端调用 api 请求。 I also used Postman and nothing happened.我也用过 Postman 也没有任何反应。
My guess: the backend either doesn't work at all, or else I use process.env.PORT || localhost:5000
我的猜测:后端要么根本不工作,要么我使用
process.env.PORT || localhost:5000
process.env.PORT || localhost:5000
incorrectly. process.env.PORT || localhost:5000
不正确。
I really need help with this.我真的需要帮助。 Sorry for my english.
对不起我的英语不好。 Thank you.
谢谢你。
I solved it by pushing frontend and backend to different apps.我通过将前端和后端推送到不同的应用程序来解决它。
For each folder, I would have 1 Procfile that has web npm start
对于每个文件夹,我将有 1 个具有
web npm start
的 Procfile
And then, I would change my API in my frontend from localhost:5000/api/info
to myappname.herokuapp.com/api/info
然后,我将前端的 API 从
localhost:5000/api/info
更改为myappname.herokuapp.com/api/info
Explain: I believe 'web' process-type is the only special one that accept external HTTP. So my backend previously did not work, while my frontend (web) works.说明:我相信“网络”进程类型是唯一接受外部 HTTP 的特殊进程类型。所以我的后端以前没有工作,而我的前端(网络)工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.