Last updated on March 3, 2021, 6:21 a.m. by rugved
I still remember as a beginner i always wanted to host my website. I was just obsessed with the thought of building a website and having a url which i can visit from anywhere. Lets see how to do it for free.
In this blog with minimum steps we will host node js website on heroku. Heroku docs are pretty good but i as a beginner found it difficult as it explains everything in detail providing unnecessary explanation which scared me away as a beginner. This blog is contains only necessary and easy to follow steps to host node.js app
Even though we are going to host simple node.js app but you can extend it for node apps containing multiple routers,controllers,database etc.
If you are need database in your app you can use mongodb atlas.
node -v
mkdir nodejs-heroku
cd nodejs-heroku
npm init -y
npm i express
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
/*
Incase you are using mongodb atlas database uncomment below line
and replace "mongoAtlasUri" with your mongodb atlas uri.
*/
// mongoose.connect( mongoAtlasUri, {useNewUrlParser: true, useUnifiedTopology: true})
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})
node index.js
Open “package.json” file and you will see “scripts” key.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Replace it with:
"scripts": {
"start": "node index.js"
},
To understand why we did this, read the following and you will see that to tell heroku what to execute we need to add either make Procfile or add command we want to execute into “start” tag in “package.json”.
https://devcenter.heroku.com/articles/deploying-nodejs#prerequisites
{
"name": "nodejs-heroku",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"engines": {
"node": "10.x"
},
"dependencies": {
"express": "^4.17.1"
}
}
Here i am not explaining each command because i am assuming you know basics of git. Even if you don’t know just execute them for now.
git init git add . git commit -m "first commit"
git remote add origin git@github.com:RugvedB/Nodejs-Heroku-Deploy-Demo.git
git push origin master
We did this so that we can later tell heroku to refer to this github repository for code.
https://github.com/RugvedB/Nodejs-Heroku-Deploy-Demo
heroku login
Even thought its a very simple app but the above steps can be followed to build apps with complex structure too.
Yay! That’s all is needed to host a node.js app.
Everything is done. You don’t need to follow this unless required.
Here is answer to one of the most frequently asked question.
If you have added some secrets like email,password, mongodb atlas uri etc then in the code you shouldn’t direct paste it, instead put the following in the code and set its value in heroku “config vars” section here.
Eg
// In the code process.env.EMAIL_PASSWORD
Setting config vars on settings page
Thank you! Drop a like if you found it helpful!
We successfully learnt how to deploy node.js app and how to set config vars on heroku.
For code you can visit https://github.com/RugvedB/Nodejs-Heroku-Deploy-Demo