Deploy a Node Application on the Heroku Server

Before we begin deploying our app on the Heroku server be sure that you have a registered a Heroku account, if not then click here to register.

Note: You must have Node and GIT installed on your system before we can deploy our app.

For this example I assume that you have a basic server file created using Node ready with you. If not, then the code for server.js is as follows.

  1. var http=require('http');    
  2.     
  3. var server=http.createServer(function(req,res){    
  4.    res.writeHead(200,{'Content-Type':'text/html'});    
  5.    res.end('Hello World');    
  6. });    
  7.     
  8.    var port=process.env.PORT || 3000;    
  9.     
  10.    server.listen(port);    
  11. });   
Rather than running that file from our localhost server, we will upload it on the Heroku server and run it from there.

Before we start to upload this file on the Heroku, we need to make some changes. We cannot set the port on our own, it might already be set in the environment variable. So we will check if the port number is already set else we can use our own port number. We will save the port number in a port variable and pass that variable in the server.listen() function. The updated code will look like this.

 

  1. var http=require('http');    
  2.     
  3. var server=http.createServer(function(req,res){    
  4.    res.writeHead(200,{'Content-Type':'text/html'
  5. });    
  6.    res.end('Hello World');    
  7. });    
  8.     
  9.    var port=process.env.PORT || 3000;    
  10.     
  11.    server.listen(port);    
  12. });  
So now we are ready to create the dependency files and uploading our application on the Heroku server. Go to the folder with your server.js file.

Step 1

Heroku requires a package.json file. We can create it with the “npm init” command. You can leave everything to its default.

npm init

Step 2

Hero also needs a second file called “Procfile” without any extension that tells Heroku how to start your application. So inside your current directory create a file called Procfile with the following content and save it.
  1. web: node server.js   
Step 3

We will be using GIT to upload the files on the Heroku server. So we need to initialise GIT on the current folder, add all the files and commit them.

package

Step 4

Do a Heroku login using your email id and password.

heroku login

Step 5

In this step we will create our Heroku app with a unique name, we will use “Heroku create” for that.

heroku create

We have named our app “hello-world-121″ and you can see that that a unique URL has been generated for our app.

This URL has no code yet, so in our final step we need to push our code from our local system to the Heroku server using the “git push” command.

Step 6

Push our code to the server.

code

We have named our app “hello-world-121″ and you can see that a unique URL has been generated for our app.

This URL has no code yet, so in our final step we need to push our code from local system to the Heroku server using the “git push” command.

Step 7

Push our code to the server.

output

This was a very basic example of how to upload your app to the Heroku server.

 

Up Next
    Ebook Download
    View all
    Learn
    View all