Go live with your node js app in 8 mins 12 secs

Winning a coding hackathon, broadcasting your POC to bigger audience or maintainig your pet projects online - a live software in production always appeals more to the intended users. You need not have to worry about the tedious configurations that often comes with hostings and deployments. Say bye bye to deployment docs with heroku. Deploy your existing/new NodeJs apps to Heroku platform for free in a matter of minutes. Get a herokuapp domain and a free dyno.

deploy node apps to heroku

Heroku is one of the leading hosting solutions, deploying node js to heroku has not been this easier. Follow along with me for the rest of the post for quickly deploying your node js application in less that 10 commands.

Git set up and code changes

  • Create an empty git repository
  • Clone this git repository locally
  • $ git clone "gitUrlOfTheRepo"
  • Update the empty cloned git repo by making it a basic node js appliation.
  • Add/Update the package.json of the app with engines property, add node and npm versions. These will be node and npm versions running on your deployed app. (Check the local versions of your node and npm and update accordingly). Finally package.json should look somthing like following
  • {
      "name": "nodejsheroku",
      "version": "0.0.1",
      "description": "deploying node js to heroku platform",
      "main": "server.js",
      "author": {
        "name": "Nishanth Kabra",
        "email": "[email protected]"
      },
      "engines": {
        "node": "4.4.x",
        "npm": "3.8.x"
      },
      "dependencies": {
        "express": "4.13.x"
      }
    }
    
  • Create a file with no extension and name it "Procfile". The content of the file will be trigger for running the node js app. Update it with the following command. (Here server.js is the root of my node app)
  • "web: node server.js"
  • Create a simple node server by adding a file server.js and the contents of the file will be
  • var express = require('express');
    var app = express();
    var port = process.env.PORT || 3000;
    
    app.get('/', function (req, res) {
        res.send("Hello dev, am a local route nobody knows me yet!");
    });
    
    var server = app.listen(port, function () {
        console.log('node server is just fine! and running on port - ' + port);
    });
    
  • Install the dependencies from command line $ npm install
  • Run your node appliation locally from command line$ node server
  • Now push all these changes to git repo from command line.(One should not push node modules and any other files which are merely dev dependencies to git repo - do not forget to add .gitignore file later to this project).>
  • $ git add -A
    $ git commit -m "initial commit"
    $ git push
    

Heroku commands

  • Naviagate to your node js app locally using command prompt and login in to heroku from your command line by typing heroku login. Enter the user name password of your heroku account.
  • $ heroku login
  • Create a heroku app by simply typing heroku create. This creates a heroku app, gives you a public url and also add git remotes for your heroku app.
  • $ heroku create
  • You can see the git remotes by typing.
  • $ git remote -v
  • If you have to set any environment varaibles for your node app, you can set it now by adding key value pairs into the heroku config object
  • $ heroku config
  • By now you can go to your personal heroku dashboard and see you node app listed there. Heroku dashboard
  • Finally push all these changes to heroku git. Your app builds here.
  • $ git push heroku master
  • At this stage your node app is now ready to go live and waiting for resources. You should now be allocating resources by typing the following command. This allocates a single dyno to your node application(You can increase the number of dynos by upgrading your free heroku account).
  • $ heroku ps:scale web=1
  • Done! wanna see your live node js appliation, type heroku open
  • $ heroku open

Updates and future deployments

  • Often you may get issues deploying for the first time, do not worry and try re deploying.
  • Once you succefully deploy your app, the next update only requires
  • $ git push origin master
    $ git push heroku master
    $ heroku ps:scale web=1
    

Feedback

If this post helps you in deploying your node js application to heroku, do share.