Deploy a node application on the Heroku server

Before we begin deploying our app on the Heroku server make sure that you have a registered 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.

In the previous post we created a server.js file to display “hello world” on the screen. Rather than running that file from our localhost server, we will upload it on the heroku server and run it from there.

The server.js file looked like this

var http=require('http');

var server=http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.end('Hello World');
});

server.listen(3000,function(){
console.log("server running on localhost:3000");
});

Before we start to upload this file on the heroku, we need to make some changes. We cannot set port on our own, it might be already 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.

var http=require('http');

var server=http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.end('Hello World');
});

var port=process.env.PORT || 3000;

server.listen(port);
});

So now are ready to create the dependency files and uploading our application on the heroku server. Goto the folder with your server.js file.

Step 1: Heroku requires a package.json file. We can create it us “npm init” command.You can leave everything to its default.

Screen Shot 2015-06-04 at 10.18.06 pm

Step 2: Hero also needs a second file called “Procfile” without any extension which tells heroku how to start your application. So inside your current directory create a file called Procfile with the following content and save it.

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.

Screen Shot 2015-06-04 at 10.27.50 pm

Step 4: Do a heroku login using your email id and password.
Screen Shot 2015-06-04 at 10.33.10 pm copy

Step 5: In this step we will create our heroku app with a unique name, we will use “heroku create ” for the same.

Screen Shot 2015-06-04 at 10.42.02 pm

We have named our app “hello-world-121” and you can see that that a unique url has been generated for our app.
https://hello-world-121.herokuapp.com/

This url no code yet, so in our final step we need to push our code our local system to the heroku server using “git push” command

Step 6: Push our code to the server.

Screen Shot 2015-06-04 at 10.46.55 pm

Now if we check the app url https://hello-world-121.herokuapp.com/ we will see that our app is running on the heroku server.

Screen Shot 2015-06-04 at 10.48.00 pm

This was a very basic example of how to upload your app to the heroku server. In our coming posts we will move into more complex apps.

Related Posts