DEV Community

Cover image for Day 6 (last) - Your first week in NodeJS
Gurpreet Singh
Gurpreet Singh

Posted on • Edited on • Originally published at learnwithgurpreet.com

Day 6 (last) - Your first week in NodeJS

Overview

During the course of this series, we have already developed a Node.js application that can be deployed on the cloud.

There are some providers which provide cloud hosting for Node.js applications, we will be using one of those called Heroku.

Heroku is one of the good options I found in the market which comes with a free DEV account, which you can leverage to start working on small projects or POCs.

Start with account creation

To use Heroku, you defiantly need an account which can be done from here.

Once you have your Heroku account up and running, please create a project after login into your account.

create_new_project
All you need is a unique name here which will become a sub-domain of Heroku, in my case, it will be https://not-failing.herokuapp.com/

Start with Heroku git setup

Heroku-hosted projects are also based on a git revisioning system, you can simply take checkout of your project and commit your changes.

Since we already have our project developed, we can simply convert it into a Heroku project.

Install the Heroku CLI

Download and install the Heroku CLI.

If you haven't already, log in to your Heroku account and follow the prompts to create a new SSH public key.

$ heroku login
Enter fullscreen mode Exit fullscreen mode

Create a new Git repository

Initialize a git repository in a new or existing directory

$ cd my-project/
$ git init
$ heroku git:remote -a not-failing
Enter fullscreen mode Exit fullscreen mode

Deploy your application

Commit your code to the repository and deploy it to Heroku using Git.

$ git add .
$ git commit -am "make it better"
$ git push heroku master
Enter fullscreen mode Exit fullscreen mode

Existing Git repository

For existing repositories, simply add the heroku remote

$ heroku git:remote -a not-failing
Enter fullscreen mode Exit fullscreen mode

Since we have the .env file on our system and node_modules, don't forget to create a .gitignore file with the following content.

You would also need to migrate your .env file variables and their values to Heroku environment variables.

There are two ways to set them, let's check the command line.

$ heroku config:set MONGO_HOST=my_hostname
$ heroku config:set MONGO_PASS=my_password
$ heroku config:set MONGO_USER=my_username
$ heroku config:set MONGO_DB=my_db_name
Enter fullscreen mode Exit fullscreen mode

You can later check your config variables by typing $ heroku config:get MONGO_HOST

Before pushing your code you need to make the following changes in the app.js file.

// app.js

// Add a condition to render "dotenv" package only on local
if (process.env.NODE_ENV !== "production") {
  require("dotenv").config();
}

// Get PORT from process variables (to get Heroku PORT)
const PORT = process.env.PORT || 3000;

// Replace hardcoded PORT with variable.
app.listen(PORT);
Enter fullscreen mode Exit fullscreen mode

I believe your App is up and running on Heroku now, like mine :)

heroku_app

Hope you liked the series, feel free to drop a comment in case you want me to create another series on another topic.

Useful Links

Top comments (0)