How to run Node.JS server for a web application?

Ramanpreet Singh picture Ramanpreet Singh · May 8, 2013 · Viewed 43.3k times · Source

Info: I am very new to node.JS!

I have written a sample server that can listen to http requests on port XXXX. When I run this server from commandline (Windows) it seems to work well. It responds to the requests made to localhost:XXXX when opened in a browser.

Question: Is this how this is supposed to work? For the node server to run, should there always be a CMD prompt open for the server to listen to requests? Can I not do "something" with IISNode?

I understand that if I make a request to a JS files, which is noted in IISNode as a Node.JS file and that NODE should be handling it; then I will have Node handling the request for me. But then this assumes that IIS is the web server for me and that specific requests can be handled by Node.

I hope I am making sense here! :)

Answer

Tomasz Janczuk picture Tomasz Janczuk · May 8, 2013

On Windows you have two options of hosting node.js applications:

  1. Self-host the node.exe process just like you would on *nix. During development you will probably just start it from the command line. In production you want to come up with a mechanism that will provide process lifetime management around node.exe (e.g. start it when the OS starts). The most reasonable way of doing it on Windows is to use Windows Services (also known as NT Services). A component that can help you do this is http://nssm.cc/.
  2. Host node.js with the IIS using iisnode (http://github.com/tjanczuk/iisnode). Compared to self-hosting this method has a number of benefits outlined in https://github.com/tjanczuk/iisnode/wiki. But you also want to explore the performance implications (not all of them bad actually): http://tomasz.janczuk.org/2012/06/performance-of-hosting-nodejs.html.