Befall Befall - 1 year ago 142
Node.js Question

How to deploy live Node.js server using Heroku

Sorry for confusing title, new to Node.js and Heroku, but trying to quickly pick it up.

I currently own a domain, and my first goal is to have it set up so I go to the domain, it uses Heroku to wake up and run Node.js, run my web.js, and display "Hello world".

I've already gone through this heroku/node.js tutorial, and so I understand how to set up stuff locally, push to the heroku remote, and run my Node.js server with it (am I understanding that correctly?). However, I couldn't find anything for how you actually put your Node.js files onto your external server (so, in this case, my domain), and connect the service of Heroku to those files, allowing my local machine to interact with the node on my server.

If there's any tutorials or pages you'd recommend, I'd appreciate it. Kinda stuff and most likely confused on quite a few things here. Thanks!

Answer Source

Heroku apps have their own git repository. So, you push from your local git directory to heroku's git remote.


git remote add heroku  <git://yourherokurepourl.git>

and then every time to deploy:

git push heroku

That's all needed to get your node.js files on heroku's server. Heroku follows foreman as process launcher. foreman needs a special file in project root called procfile. procfile has simple unix commands to launch processes in each line :

web : npm install && node app.js

So, when you push your project to heroku's git. It will look for procfile and launch processes defined there. You can place more commands here. Better install foreman on your local/development machine and test using that.

In heroku app settings you can "bind" your address to node app running on heroku's server. You have to do the same in settings of domain provider. The DNS will now route requests to to your app's server IP.

On heroku, configuration lives in environment. A lot many process.env.* are available on heroku. You can locally simulate this by providing .env files to foreman.

finally, in your node.js code make sure you listen on value provided by process.env.PORT.

Connecting servers:

  1. Use Request module to directly call other server urls.
  2. Or, Let server's subscribe and publish to a centralized service bus.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download