Skip to content

Instantly share code, notes, and snippets.

@ialtafshaikh
Last active March 4, 2021 12:13
Show Gist options
  • Save ialtafshaikh/8336df5d417109b12c46bd20ccda4e17 to your computer and use it in GitHub Desktop.
Save ialtafshaikh/8336df5d417109b12c46bd20ccda4e17 to your computer and use it in GitHub Desktop.
Procedure to host the nodejs application on Heroku

Heroku Hosting - NodeJS

Pre-requisite

Logging Into Heroku

heroku login is required once

heroku login

Specify the version of node

Add The version of Node.js to Your package.json file:

"engines": {
    "node": "10.x"
},

Specifying a start script

create a Procfile file inside the root directory of the project and add the below code in it

web: node app.js

By default, Heroku will look into our package.json file under the scripts section and grab start. Sometimes we won't have that defined or it will be different than what we want the server to execute. We can specify the exact command we want by creating a Procfile

Create Heroku App and Remote Repo

heroku create

This will create a random name for our application

Rename Your APP

heroku apps:rename new-name --app old-app-name

Build your app and run it locally

heroku local web

Deploying Code

git push heroku master

If your Project has Environment Variables

Local

To provide a local development environment, create a .gitignore’d .env file, which will be loaded by heroku local:

DATABASE_URL='postgres://localhost/foobar'
HTTP_TIMEOUT=10000

Now start your app with heroku local, and it will automatically pull in these environment variables into your app under process.env.DATABASE_URL and process.env.HTTP_TIMEOUT.

Heroku / Production

Set a config var on heroku app

set all of your env variables and you are done

heroku config:set GITHUB_USERNAME=joesmith

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment