简体   繁体   中英

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.

Task: I have 2 folders frontend and backend. I have Dockerfile in both of them like this /frontend/Dockerfile and /backend/Dockerfille. I also have docker-compose.yml and Procfile on the root directory.

Attempt 1: change to Dockerfile.frontend and Dockerfile.backend. I use heroku:container push --recursive . Successfuly deploy it on Heroku, I already have scale=1 too. It doesn't work.

Attempt 2: change Dockerfile.frontend to Dockerfile.web and Dockerfile.backend. The frontend web is working, but I can't login so I can't say for sure. I can't call api request to backend with POST https://mycoolapp.herokuapp.com/api/users/login . I also used Postman and nothing happened.

My guess: the backend either doesn't work at all, or else I use process.env.PORT || localhost:5000 process.env.PORT || localhost:5000 incorrectly.

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

And then, I would change my API in my frontend from localhost:5000/api/info to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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