Search code examples
node.jsdockerheroku

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 incorrectly.

I really need help with this. Sorry for my english. Thank you.


Solution

  • 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.