paint-brush
How to Deploy an Express Node.js Application to Heroku Quickly and Easilyby@prplcode
603 reads
603 reads

How to Deploy an Express Node.js Application to Heroku Quickly and Easily

by Simon Egersand 🎈May 31st, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I'll show you a step-by-step guide to deploying your Express Node.js application to Heroku. Heroku is a Platform as a Service (PaaS) and should not be confused with SaaS. It offers a hobby plan where you can deploy your applications for free, with some limitations. The guide will use a minimal example with an Express server with an HTTP Express server.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Deploy an Express Node.js Application to Heroku Quickly and Easily
Simon Egersand 🎈 HackerNoon profile picture


With Heroku, you can deploy your Express Node.js application to production in just a few steps. In this post, I'll show you a step-by-step guide to deploying your Express Node.js application to Heroku.


To quickly get started, you can use my repo template simeg/express-heroku-example.

What is Heroku?

Heroku is a Platform as a Service (PaaS), and should not be confused with Service as a Service (SaaS). It offers a hobby plan where you can deploy your applications for free, with some limitations.


For all my hobby website projects, I use Heroku. I've created things like sudoku-js and impossible-tic-tac-toe. See the About sections for links to the applications.

Preparation

First, install the Heroku CLI. If you're on macOS, run:

$ brew tap heroku/brew && brew install heroku


Otherwise, head over to Heroku's website.

Deploy Node.js Application to Heroku

Now that you have the CLI installed, we can start writing some code. We will use a minimal example with an HTTP Express server.

Node.js Application

Bootstrap a Node.js application with npm init. Then, add Express as a dependency with npm i --save express.


Now, let's look at our slim Express server in index.js.


https://gist.github.com/simeg/52b1bf277d5fe447239b379ea41346d0


You can read more about Express here.


This HTTP server is simple. It has one GET endpoint, which returns the 200 and the text Hello World!.


Now that we have the server ready, we need some extra things to be able to deploy it to Heroku. First of all, we need a Procfile.


https://gist.github.com/simeg/5b0625a69e2f8427dd35cea94ad4fc88


This is the file that Heroku reads when it starts the application. As you can see, the file runs npm start, so we need to create that too. We add it to package.json.


https://gist.github.com/simeg/c50ff3dd100963b967dd22bc1c8c06b8


Also, notice the engines section. This is for Heroku to know what runtime to use to run your application. You can see what Node.js versions Heroku support on this site.

Deploy to Heroku

There are a few ways to deploy to Heroku. We will use git which is the easiest way.

Now that all the code is written, we need to commit it.

$ git add .
$ git commit -m "Initial commit"


Then, we need to create an application on Heroku.

$ heroku create


This command also adds a git remote called heroku. This remote is where we push to deploy our application. Let's do that now!

$ git push heroku main


At this point, Heroku will try to figure out what build pack to use. Essentially, what type of application are you deploying? Because we have a package.json file in our root, it knows it's a Node.js application.


When the command is done, it will output a URL. Let's open it!

...
https://thawing-beyond-32509.herokuapp.com/ deployed to Heroku
...


And, we can see Hello World! in the browser. Easy as pie!


Now, you can check the logs for your application.

$ heroku logs --tail

Conclusion

Now, you know how to deploy a Node.js application to Heroku. Heroku provides great tooling to quickly get something up and running. But this is just the start! Express allows you to build complex web applications. And with Heroku, you can quickly deploy them to production.


Check out Heroku's Best Practices for Node.js Development for tips and tricks. And their page about Node.js is also useful.



Connect with me on TwitterLinkedIn, or GitHub