Deploying Node.js App on Heroku

0
975

As you probably know Heroku is a web app for managing web applications that are hosted in the cloud, here you can create new apps, deploy new apps, update apps and and new add-ons like logins and error tracking.

First thing is to sign in for a new account if you don’t a have a new one yet.[/vc_column_text]

Next we need to install Heroku Command line tools, this is going to let us create, update, open and do a lot more directly form the terminal.

Visit https://devcenter.heroku.com/articles/heroku-cli and follow install instructions for you Operating system.

Then Log in into Heroku by typing heroku login in your terminal and providing your login credentials.

The next step is to get our ssh keys into Heroku by running heroku keys:add in terminal

You can print your Heroku keys by typing heroku keys on your terminal

Next, Let’s test the connection by typing ssh -v git@heroku.com in your terminal

You should see authentication succeeded in your terminal after this.

[vc_gallery interval=”3″ images=”1616″ img_size=”500×354″]

One of two important changes we need to do in your node application is to make the port dynamic by using an environment variable which Huroku is going to set once we deploy the app.

we can see the environment variables by typing env in the terminal or SET command on Windows.

In your node application you need to create a variable port like this:

const port = process.env.PORT || 4600;

process.env is an objects that stores all environment variables as key value pairs.

As process.env.PORT is likely won’t exist locally in previous line we are setting it to 4600

Then all we have to use is to use this port variable instead instead of the static port number.

Example:

app.listen(port, () => {
    console.log(`Started up at port ${port}`)
});

the second thing we need to add is a script in our package.json

we need to add:

"start": "node server/server.js"

Since when Heroku tries to start our app is not going to run node + file name because it doesn’t know what this file name is called, instead is going to run the start script which is responsible to call the right files.

In similar way can run our app with npm start after applying this change.

The next step in the process is to run a command called heroku create from the terminal, heroku create needs to be executed from inside your application just a we run our git commands.

Once we run heroku create a couple of things are going to happen:

First it is going to make a real new application over Heroku web apps, then it is going to add a new remote to our git repository this new origin is going to point to Heroku git repository, when we deploy to Heroku git repository, Heroku is going to see that, is going to take the changes and deploy them to the web.

Html code here! Replace this with any non empty text and that's it.

Finally run command heroku open in your terminal.

heroku open is going to open the heroku app in the default browser.

LEAVE A REPLY

Please enter your comment!
Please enter your name here