Heroku: Learn how to deploy your Nodejs project, set up the env/config variables and give custom subdomain to your app
Hey !! If you want to host your app on Heroku, read this article...
This blog will help you to learn how to deploy Nodejs project, how to set up the env variables and how to give custom subdomain to your app
Cool !!
1. How to deploy Nodejs project to Heroku ??
Follow the below steps to deploy your app:
A. first you have to create the Heroku account, if already present move on to the next step
Heroku Sign up link: signup.heroku.com/login
B. after creating Heroku account, download git on your system
C. after downloading git, download Heroku CLI on your system because Heroku CLI requires git
Go to this link to download Heroku CLI: devcenter.heroku.com/articles/heroku-cli#do..
D. after downloading git and Heroku on your system, check if they are installed on it
to check Git, write "git --version" on the terminal and to check Heroku, write "heroku --version"
E. after completing above steps, open your Nodejs project folder, we have to do some additions....
Follow the below procedure:
- open your code editor and make 1 file, give it the name "Procfile" and inside it, write "web: node index.js"
Note: you can replace the name "index" and give anything which you have given to your main file and make sure to follow above thing as it is
- after making procfile, write "process.env.PORT" to api which is running the server
app.listen(process.env.PORT || 3000, function() {
console.log("Server started on port 3000");
});
F we have completed almost....now write the below commands on terminal
write "git init" to initialize the empty git repository
then write "git add ." to add all files to staging area and then write "git commit -m "first commit"" to commit the files
after that, write "heroku login" and hit enter
you will be prompted to the window, where you have to sign in to Heroku and that's it
after that, write "heroku create" and your heroku app will be created
then write "git push heroku master" to push your files to heroku repository
and that's it, we have deployed our app to heroku....
wooh !!!
Now on the Heroku dashboard, you will see your app, congrats !!!
2. How to rename the app ??
See, we can only give the custom subdomain and only that from terminal. If you rename the name from your Heroku dashboard, it will create the problem because your heroku repository name will not change then, so let' see how we can
Write the below command:
heroku apps:rename naya_naam_de_do
Useful Link: devcenter.heroku.com/articles/renaming-apps
3. How to set up the env variables ??
In projects, generally there is environment file called ".env" which contains sensitive information like api secret or code or database url, so we have to save those information in ".env" file and , we can access these information in our project by using "process.env.variable_name"
If you are running your project in localhost, "process.env.variable_name" will fine perfectly but in Heroku, it won't run and your app will show application error. Thus in some hosting sites we have to set up the env/config variables specifically like in case of Heroku
Write the below command to store env/config variables to heroku so that your app will run perfectly
heroku config:set variable_name=variable_value
Useful Link-1: devcenter.heroku.com/articles/config-vars
Useful Link-2: stackoverflow.com/questions/49905070/how-to..
Wow.....
I hope, now you can easily deploy your apps to Heroku
Plz like and comment !!