Deploy Node JS app on Heroku
Deploy your rest api created using node js to Heroku.
In this demo, we will be looking at how to deploy your cool NodeJS app to Heroku. I assume that you have node app ready on local machine. You can refer my previous blog to know more about node js and rest api.
Steps
- Create
Procfile
in your project base directory and add this line to the fileweb: npm start
. - Update your
pakage.json
file. Add the below code.
"engines": {
"node": "14.x"
},
"keywords": [
"node",
"heroku",
"express"
],
3. Create an empty repo in Github.com
4. Open your project and initialise git with command git init
.
5. Add files to your local git or staging area git add .
6. Commit changes with commit git commit -m “initial”
7. Link local repo to Github repo via remote git remote add origin https://github.com/deeppatel23/api-demo.git
similar to this.
8. Push your changes to Github repo. with command git push origin {branch_name}
(use cmd git branch -a
to check current branch).
9. Open your Heroku account dashboard. Create a new app.
10. Give an appropriate name and create app.
11. Go to the deploy
section and connect your Github account. You will see your account once it gets connected and now search for the repository. Select the repository and click connect
.
12. Click Enable Automatic Deploys
so that as soon as the changes are pushed to GitHub, Heroku will pick them up and deploy.
13. Add node js build pack from the Settings
tab and save the changes.
14. Now, go back to the Deploy tab, and click Deploy Branch
at the bottom.
15. Open the Activity tab and there you can see the progress. Open the settings
tab and scroll down to the Domains and certificates
section. Here, you can see the URL of your app that was just deployed. Copy and paste that URL in the browser and… Hoooorah!!
PS: If your projects runs successfully on local machine but give some application error on Heroku, then you must check Procfile
, or Engines version
in package.json, or path of any other files listed (Note: path should start with ../{file_name}
eg: “../api-demo-file.json”
). Do check logs (Dashboard -> More -> View Logs
)for better understanding of errors.