Saturday, August 8, 2020

Deploy app to heroku

 heroku deployment.


port number will generated randomly, so set the port number with env variable.



index.js

-------


const express =require('express');

const app = express();



app.get('/',(req,res) => {

 res.send({hi: 'there'});

});



//const PORT = process.env.PORT //fetched from heroku dynamically

const PORT = process.env.PORT || 5000;

app.listen(PORT);


################

we need to tell node version also to heroku or else it uses an older version.


in the package.json add line (like just above scripts)

"engines": {

 "node": "8.1.1",

 "npm": "5.0.3"

},


#######################

what command to run to start up server to heroku.

inside scripts.


"scripts": {

  "start": "node index.js"

},


###############################

create .gitignore file

node_modules



Installing heroku cli

-------------------

create heroku.com account.

heroku provides a gitbased deployment to host code.

install heroku cli(search in google for install steps)

>heroku -v

>heroku login

>heroku create -->generates two urls,1st is app url and 2nd one is git target link.

link the 2nd link to the project code you kept as local git repo.

after git push.

>heroku open --> launches deployed app on browser.

>heroku logs


No comments:

Post a Comment