6 Steps to Deploying Node.js Application on Heroku
What is Heroku?
Simply put, Heroku is a cloud platform that lets companies/individuals build, deliver, monitor, and scale apps. It is often regarded as the fastest way to go from idea to URL, bypassing all those infrastructure headaches (i.e, you do not have to worry about infrastructure; you just focus on your application).
What Heroku offers
Heroku offers PaaS type of cloud computing, the delivery of computing services — servers, storage, databases, networking, software, and more — over the Internet (“the cloud”).
Operating at the layer above raw computing hardware, whether physical or virtual, PaaS provides a method for programming languages to interact with services like databases, web servers, and file storage, without having to deal with lower level requirements like how much space a database needs, whether the data must be protected by making a copy between 3 servers, or distributing the workload across servers that can be spread throughout the world. Typically, applications must be written for a specific PaaS offering to take full advantage of the service, and most platforms only support a limited set of programming languages.
There is no doubt that Heroku takes away all the pain of installing software, configuring servers, maintaining it, and monitoring the software — it takes care of most of the configurations for you. Other services could easily be added as add-ons.
- Easy deployment
- It uses git commands
- Plenty of Add-on resources (applications, databases etc.)
- Processes scaling — independent scaling for each component of your app without affecting functionality and performance
- Isolation — each process (aka dyno) is completely isolated from each other thorough Documentation
Deploying your app in 6 steps
If you don’t have a Node.js app, click here to see how to create one in 10 minutes.
Here are some prerequisites you need to deploy your app:
- Node.js and npm installed.
- A Node.js app.
- A free Heroku account.
- The Heroku CLI.
Let's get down to it!
Login to your Heroku via the command line
heroku login. This will prompt for your credential.
Add Procfile to your application
echo ‘web: ./node_modules/.bin/forever -m 5 server.js’ >Procfile
Note: A Procfile is not necessary to deploy apps written in most languages supported by Heroku. The platform automatically detects the language, and creates a default web process type to boot the application server.
In the case of Node.js, it will start a default web process via the start script in your package.json.
Creating an explicit Procfile is recommended for greater control and flexibility for your app.
As the case maybe,
web: ./node_modules/.bin/forever -m 5 server.js, I am using Node.js' forever module to start my server named server.js
Create Heroku remote repository
heroku create appName
Add the new changes made to the app
git add .
Commit the snapshot
git commit -m “Added a Procfile.”
Push to your app to the newly created heroku remote repository
git push heroku masterBonus: run
heroku logsto see.
Note: If your app runs on a database, you will need to add the database as an add-on.
Visit this link for a list of add-ons on heroku.