DEV Community

Cover image for Deploy An Express Node.js Application to Heroku Quickly and Easily
Simon Egersand 🎈
Simon Egersand 🎈

Posted on • Originally published at prplcode.dev

Deploy An Express Node.js Application to Heroku Quickly and Easily

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
Enter fullscreen mode Exit fullscreen mode

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.

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.

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.

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"
Enter fullscreen mode Exit fullscreen mode

Then we need to create an application on Heroku.

$ heroku create
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
...
Enter fullscreen mode Exit fullscreen mode

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

Now you can check the logs for your application.

$ heroku logs --tail
Enter fullscreen mode Exit fullscreen mode

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 Twitter, LinkedIn, or GitHub

Originally published at prplcode.dev

Latest comments (2)

Collapse
 
maxfindel profile image
Max F. Findel

One thing I love about Heroku is that you can create lots of small projects and host them for free <3 The web "dynos" (that's how they call them) go to sleep when you stop using them and they don't count agains the "free dyno hours" they give out for free every month.

I always recommend deploying an express API like the one you described, a Strapi headless CMS or maybe something more complex like a Medusa e-commerce backend on Heroku. This pairs very well with Next.js and static websites. Specially if you deploy these on Vercel, Netlify or CloudFlare for free :)

Collapse
 
simeg profile image
Simon Egersand 🎈

Yes, exactly! Heroku is perfect for prototyping projects. And so easy to use.