Node.js is one of the fastest-growing and most enthusiastically supported technologies for creating Web applications. That, in turn, makes it a good match to deploy for an IIS installation. But the exact ways in which Node.js applications and Internet Information Services work together -- and on their own -- can make this installation a little tricky.
Few such applications talk to the Internet directly because of the risks and the workload involved. Instead, any traffic to or from the Web at large is piped through a proxy of some kind -- Nginx, for instance, or Microsoft's own IIS.
If you just want to run Node.js in a standalone fashion for the sake of development, that isn't hard: Launch the app in question and point a Web browser at it. Launching a deployment of a Node.js under IIS is another story.
Fortunately, Microsoft, which now takes Node.js pretty seriously especially on Azure, and some ambitious third parties have come up with solutions to host Node.js apps elegantly within IIS. The most fundamental and useful is iisnode, an open source project that takes the vast majority of the heavy lifting out of such integration.
The main benefit is that iisnode is a native IIS module, not a shim of some kind. It also handles many types of integration between a Node.js project and IIS, including the following:
- IIS configuration management is used. Node.js projects are managed just like any other IIS site, so you don't need to use a separate management tool. Everything can be handled through IIS Manager.
- Processes and process recycling are handled automatically. One common issue with Node.js happens if you update files for a Node.js project. You have to restart the Node.js process by hand to get the changes to take effect. Python apps under IIS, for instance, tend to suffer from the same problem.
- It offers multi-core scaling. Node.js runs single-threaded and therefore on a single CPU core. Iisnode can create a Web garden out of multiple Node.js processes, so that incoming traffic can be load-balanced across each of them. Spawn as many processes as you have cores, and you'll be able to satisfy that many incoming requests at once. Note that this doesn't take into account the thread safety of your code -- that's your job.
- It supports debugging via the node-inspector tool. Running an app in any stack isn't useful unless you have robust debugging tools; iisnode includes the node-inspector tool for detailed debugging work -- breakpoints, object inspection and much more.
Issues with hosting Node.js in IIS
A common complaint about hosting Node.js under IIS -- one which extends to many other language runtimes including .NET -- is how the first request to Node.js apps takes a long time to return a response. The first time such a request comes through, the Node.js instance has to be "spun up," which can sometimes take quite a lot of time if you have particularly complex Node.js applications. The creator of iisnode, Tomasz Janczuk, has some instructions on how to deal with this problem. Fans of clever solutions will appreciate this: It involves generating a fake request at startup, which is done entirely through IIS's configuration file for the site.
Another way to accelerate performance, often drastically, is to replace Node.js's own HTTP server with Windows and IIS's HTTP.SYS, as Janczuk explains. This means you don't have to add an extra layer of routing through IIS, although this comes at the cost of not having IIS's management features. Therefore, this should be considered an advanced exercise -- the kind of performance-optimization that should come later in the development cycle, not earlier.
Janczuk has also created Edge.js, a library that allows Node.js applications to talk to.NET within the same process. If you already have .NET libraries you've built for other projects and want to make your new Node.js project talk to them, this is one possible way to go. C#, F#, Python, T-SQL and PowerShell are all callable through Edge.js.
Finally, if you're building a Node.js project from scratch and are thinking about deploying it into the cloud, one way to do this with the help of Microsoft's framework tools is to use WebMatrix, Microsoft's free tool for Web development project management. It supports Node.js as a first-class component and you can deploy it to any hosting provider that supports it, including Windows Azure.
About the author:
Serdar Yegulalp has been writing about computers and information technology for more than 15 years for a variety of publications, including InformationWeek and Windows Magazine. Check out his blog at GenjiPress.com.