How to Deploy Node Express API to Heroku
So you’ve built an awesome API using Express with Node.js and now you want it to be public for any service or web application to use.
Prerequisites:
For MAC users
- Have a free Heroku account — https://signup.heroku.com/
- Node and NPM installed on your mac, if you don’t have them installed checkout this article
- Have the Heroku CLI tools installed with brew (if you don’t have brew ):
brew tap heroku/brew && brew install heroku
Steps Breakdown
- Create local Node Express API (using your terminal, if you don’t have an API already)
a. Create a project working directory or folder -$ mkdir node-express-api-heroku
b. Go into the new project folder$ cd node-express-api-heroku
c. Initialize this folder as a Node project$ npm init -y
d. Create files index.js, .gitignore files$ touch index.js .gitignore
c. Install Express and Cors with$ npm install express cors
2. Add configurations to package.json
3. Add your routes and configurations on index.js
4. Initialize Git, stage files, commit your changes and change branch name to main
$ git init
$ git add .
$ git commit -m 'first commit message'
$ git branch -M main
5. Connect your Heroku account to your machine
$ heroku login
Use heroku login
this will open a browser and will authenticate with Heroku
6. Create Heroku Instance
$ heroku create
7. Push your Node Express API to Heroku
$ git push heroku main
8. Visit your Heroku instance with a public link provided by Heroku
It could be something random like:
https://fast-woodland-77884.herokuapp.com/
Enjoy!!!! 🎉
Leave some comments and or question if you get stuck, would love to read your feedback 😎
Sources:
https://medium.com/geekculture/deploy-node-applications-on-heroku-a89ed51e0a34