Deploy an Express API (Node JS) on Heroku
Hey everyone! Today I brought you a different type of article. This is about deployment! A practical guide.. Although we develop applications on local machines, we should get and experience on deployment of an app on a remote server or host also. It will be a nice experience because you can give a public URL to anyone to access and see your work!
This is my live API on Heroku. Check it by adding api/users at the end of URL.
https://ex-rest-api.herokuapp.com
This is my live API on Heroku. Check it by adding api/users at the end of URL.
https://ex-rest-api.herokuapp.com
I'm going to use a well know cloud application platform called Heroku in my deployment process. If you don't know about Heroku, visit below link to get an idea.
Link: https://www.heroku.com
To start our task, you should have some prerequisites.
- Express API should be implemented
- Heroku CLI should be installed
- GIT should be installed
- Remote MongoDB should be integrated into application
Step 1 - Clone API
So, you can get an express API which I have already implemented from this GitHub link. Clone it and keep ready for modifications.
Step 2 - Install Heroku CLI
Install Heroku CLI from here according to your OS. CLI means Command Line Tool that is used to perform heroku commands via terminal/cmd.
Step 3 - Connect MLab as remote DB
My GitHub application has been already connected to a remote MongoDB service instead of local database. You may have already heard MLab..That's the DB as a service I'm using here.
Link: https://mlab.com
Register in this site and create a database to be used in any MongoDB application. Then connect the DB as they have defined in their site. Basic format or DB URL is;
mongodb://DBUser:DBUserPassword@DBServer/DBName
NOTE: Don't use characters like @, _, - when you define password for users.
Step 4 - Deploy on Heroku
Now we are ready to start deployment! Go into your project and check whether this configuration is there.package.json
there should be "scripts" section with "start" key. Give your main JS file runs.
"scripts": {
"start": "node app.js"
}
This will take care of starting your application after deployment. This must be correct according to your app! Otherwise Heroku fails.
Now open your terminal/cmd and go into project folder. Then follow the below commands.
NOTE: Second command push the node_modules folder into .gitignore file. Then It will not be uploaded via GIT into Heroku.
Now it's time to create an application on Heroku with a preferred name. It will create an application with a URL like https://app_name.herokuapp.com... If it says that this name is already taken, give another!
heroku create express-api
Now all are set to push the application to Heroku. Perform this command!
git push heroku master
It will take some time.. Heroku will check on start scripts we configured earlier. Then it builds node_modules in the remote host by running npm install automatically. If all the steps have been completed successfully, now you app is ready to be used in public.
Go to https://app_name.herokuapp.com URL and see the response is coming! In my express API I have set a route for home.. Then it will return a string "Hello from server".
You can check whether it's coming! If it displays an error page, find the errors via Heroku logs..
Hit this command to see logs on terminal. => heroku logs
Ok guys! If you have followed me, now your API should be up and live on Heroku! Isn't it great? You can access it in any application now! No need to run a local server to access in applications..
If you want to modify code and update Heroku?
Just go into the local project folder and do the changes. Then save all changes. Run these GIT commands and finally push it again to Heroku.
git add .
git commit -m "Add changes"
git push heroku master
So, now we have come to the end of our application! This is not so hard thing to do guys! Try this!!!
Heroku has implemented which can be used by any developer easily. No hard things! Try this out and give me suggestions..
Good Luck!
10 Comments
Thanks for sharing this useful information with us.
ReplyDeleteFull Stack online Training
Full Stack Training
Full Stack Developer Online Training
Full Stack Training in Hyderabad
Full Stack Training in Ameerpet
Full Stack Training Institute
Thanks for your feedback Vishnu
DeleteValuable post useful for everyone.Keep on sharing.
ReplyDeleteNode JS Online training
Thanks for your feedback..I will!!!
DeleteThank you very much boss! :D
ReplyDeleteThanks for sharing this blog. The topic you have described is very useful
ReplyDeleteNode JS Online training
Node JS training in Hyderabad
Than you very much for your encouragement!
DeleteThanks for posting such a Useful information .You have done a great job.
ReplyDeleteMern stack online training
Mern stack training in Hyderabad
Thank you very much!
Deletetricinar-do Ana Baker https://wakelet.com/wake/a8e_48biy4h135eDjS4up
ReplyDeletelightasubse