Deploy Node JS

Deploy Node JS app on Heroku

0
(0)

When it comes to deploy Node JS app, Heroku is by far the most feasible choice for beginners and startups. In this tutorial, we are going to teach you how you can deploy your Node JS application on Heroku and make it accessible from everywhere.

It is very common when we start building our Node JS apps, we mostly work on our localhost for faster testing. But when there is a time to make it live or connect it with your android or iOS application, you definitely need to publish it on some hosting provider. Heroku in this regard will help us to publish our Node JS app on their server.

Let’s get started

We are going to create a simple “Hello world” app in Node JS and publish it on Heroku.

First create a new folder where we will place all our Node JS files. If you are already working on some Node JS project, then you can use your existing folder too. Create a file named “server.js”, this will be our main server file. Open the command prompt in your project and initialize NPM in it, make sure you have Node JS installed in your computer. You can download Node JS from here:

cd "your_project_root_folder"
npm init

Install modules

Install express and Http modules in your project, they will be used to start the server at a specific port. After installation, we are going to start the server using nodemon command. If you do not have nodemon, you can install it by running “npm i nodemon” in your terminal. Run the following command in your terminal to install express & Http modules and start the server:

npm install express http
nodemon server.js

Now we will create instances of express and Http modules and start the server at port 3000. Specifying the port will work on the localhost, but when we deploy any app on Heroku, Heroku automatically assigns some port number from itself and save that port number in the “process” object’s “env” object. So we will add an OR operator to set port number if it is being received from Heroku, otherwise (if in localhost) start at 3000. Paste the following code in your server.js file:

var express = require("express");
var app = express();
var http = require("http").createServer(app);

http.listen(process.env.PORT || 3000, function () {
	app.get("/", function (request, result) {
		result.send("Hello world !");
	});
});

If you test the app now at URL “http://localhost:3000/” you will see a “Hello world !” message. Now we need to deploy on Heroku, goto Heroku and login or signup if you have’nt already created an account.

Deploy Node JS on Heroku

Now download and install the Heroku CLI. Run the following command in your terminal to login, it will open the browser, just hit “Login” in opened web page. Also make sure that you have Git installed too, you can download it from here:

heroku login

// Initialize a Git repository
git init

// Point remote address towards your Heroku app
heroku git:remote -a your-app-name

// Add all files in Git
git add .

// Commit all files
git commit -am "Initial commit"

// Deploy all files on Heroku
git push heroku master

After the final command, you will see a link of your app which you can use to run directly in the browser. Moreover, in your Heroku dashboard, you can click on “Open app” button and it will take you to your app’s home page.

Learn how to deploy Node JS app with Mongo DB and Vue JS from here.

How useful was this post

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post





Leave a Reply

Please disable your adblocker or whitelist this site!