Deploying Node.js server on Heroku Cloud

As a developer I have been always curious to learn and try new technologies that can improve my skill and help me build better Web Applications..So today I'm going to try help you set up a simple Node.js server on Heroku Cloud using my small open source project aims deploy even faster Node.js applications.

Why Heroku

<a href="http://www.heroku.com" target=""_blank title="">Heroku is a cloud application platform where you can build and deploy web apps with the technologies you know as Java, Ruby, Closure, Node.js, Python and Scala..Heroku uses integration with Git so you can deploy your Application simply pushing to master. Oh and It's FREE!

I have four web apps hosted on heroku but two are for public access this blog/portfolio running on Ruby and Lollagram running on Node.js.

Why Node.js

Javascript is well known by us Front End Developers/Engineers.

That's already one good reason but the main "real" reasons are Node.js is all about making event-driven (Ajax can be done on the server side), low-latency, non-blocking I/O...perfect for real time.

Node.js makes a much smaller footprint on your web server It allocates web server resources on an as-needed basis, not pre-allocating a large chunk of resources for each user. For example, Apache might assign 8MB to a user, while Node assigns 8KB.

Heroku set up

I'm going to assume you have account on Heroku (it's free) and the toolbelt kit installed and you also have Node.js installed.

Let's get it start

First of all (after everything installed) create a new folder (whatever-name-you-want) then open your terminal go to your folder and type..

git init

then

git add .

then

git commit -m "first commit"

Now you have git initialized..time to create a Heroku app.
First thing you need to login to Heroku (remember that we are doing everything from the command line on terminal)

heroku login
heroku create name-of-your-app

Done! You just need to push your code.

git push

You can access your Web App url on www.your-app-name.heroku.com

Oh wait! What is this?

heroku 404

To make your Web App run on Heroku you need to run your Web App in one of those languages I said right on the beginning of this post.

Today we are going to run and deploy a simple Node.js/Express.js Application..since I have been using Heroku and Node.js for all my personal/aside projects I created a Express.js boilerplate to run and deploy fast and easy with Grunt.js included so you can watch for changes on your files to minify CSS and uglify JS. I also included static assets and NO Jade (jade is native on Express.js) so you can code your normal HTML instead of Jade.

Go to my Github repository Express.js static boilerplate and download the zip file then extract and copy everything has inside of "expressjs-static-boilerplate" folder to your Web App folder.

Running Node.js

After download my boilerplate let's install the dependencies and run our Node.js server

sudo npm install -g

Enter your password. You should see something like the image below.. notice that not every log is showed on the image..

success image

All Node dependencies are installed and now you can start run your server.

node server.js

Or to not need to quit the server "control + c" every time you do any change I also added a monitor to the boilerplate so every time you do your changes and save the monitor you automatically re-run the server.

nodemon server.js

Running Grunt.js

After install all the Node.js dependencies you can start run your tasks to minify CSS and uglify JS.
Open a new tab on your terminal and run the command.

grunt build

This is the message you should get.

Grunt build

But still not quite good enough because we would need to build every time we did any change. So let's start to watch for changes on the files.

grunt watch

Now every time you do any change and save the file grunt you build automatically.

Grunt watch

Ok. That's pretty cool right?!.

Deploying and Running on Heroku

Now let's push to Heroku and see if it works.

git add .
git commit -m "added express boilerplate"
git push origin master

and this is what happened..

no support

If you read the message you will see "Push rejected, no Cedar-supported app detected" so let's add support to make this baby work.. Every Web App on Heroku needs a file to tell Heroku what are we deploying..

In this case we need a "Procfile". So create a new file named "Procfile" and type "web: node server.js" save inside your app folder.

Then you need to activate your server and tell Heroku to run 1 "Cedar" (one cedar is free hosting).

heroku ps:scale web=1

no support

Repeat the push process.. after do the push process again "git add ." "git commit" and "git push origin master" a bunch of message will log and the final logs would be like this image below.. now you can access your Web App URL in my case is deployed-nodejs-app.

Web app deployed

And this is how we deploy Node.js to Heroku.. fast and easy.

Cheers!


More Stories

Cover Image for Using ChatGPT as your nutritionist

Using ChatGPT as your nutritionist

While ChatGPT is primarily known for its conversational abilities, it can also serve as a virtual trainer and nutritionist, providing personalized guidance to help you achieve your health and wellness goals.

Cover Image for How to invest in the stock market - a beginners guide

How to invest in the stock market - a beginners guide

There is so much bad information on how to invest in stock market causing a lot of confusion for people that want to get started in investing that I decided to create a guideline for beginners based on Peter Lynch work