Deploying node on IIS

Summary

So my node blog is up and running, obviously, but deployment was a bit frustrating. I wasn't planning on posting about this, but hopefully someone else might find it useful as I had to piece together suggestions from all over to get this working.

Heroku

I really wanted to get this site working on Heroku, I mean really wanted to get it working. I love how easy it's supposed to be to deploy via git push. I followed official tutorials, I followed unofficial tutorials, and spent a lot of time on stackoverflow. The problem I kept running into was dealing with public keys. I tried various suggestions for recreating, reinstalling, clearing, adding, sacrificing chickens, but in the end this blog wasn't worth enough of my time to spend over three hours on what should be a simple task. Maybe my issue is Windows, maybe it's just me, either way I gave up and went looking for a solution on IIS.

IISNode

IISNode[1] is a module that allows IIS7 (and above) to host node.js applications through the windows node binary. Minimal configuration is needed, and for those familiar with hosting .NET sites you get to create a web.config file, yay!

Getting the files from github to the server

Eventually I would like to make use of git's push hooks to create a Heroku-like environment, but for now I doing it caveman style by logging into the server and interacting with git.

I've been using Github's git client for awhile, and for simple projects where I'm the only dev it works flawlessly between my office PC and virtual machine on OSX.

I installed Github's client on my server (Windows 2012) and cloned the my blog repository to the www directory used for serving my sites. From this point on whenever I update the project on Github I can simply log into the server, click the "sync" button in the git client, and let git update my source. The great thing about iisnode is that it gracefully refreshes the site on file updates so nothing else is required after syncing to see the latest version in the browser.

Setting up node.js, IISNode, and IIS by the numbers

  1. Download & install the latest node.js binary from http://nodejs.org
  2. Pull down the source for the website from Github, local SVN, zip file, whatever!
  3. Open a command prompt and use the following command npm install -g npm-install-missing.
    1. In the command prompt navigate to the directory where the site's source and package.json resides
    2. Use the command npm-install-missing to download all dependencies for the site.
  4. Download & install the latest iisnode build from https://github.com/tjanczuk/iisnode/releases (2.15 x64 at the time of this post).
  5. Create a new website in IIS
    1. modify the site bindings. I've setup upnxt.com and www.upnxt.com both on port 80 for this site, though you should be able to bring your application up by IP, or modify your host file to bring up the site by name.
  6. Set the application pool to run as LocalService
  7. Make sure the Url Rewrite module is installed in IIS. If you can't find it use the web platform installer and search for url rewrite 2.0
  8. Create a web.config file and place it in the root of the node app with the following configuration

<configuration>
    <system.webServer>
        <handlers>
            <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
        </handlers>
        <rewrite>
            <rules>
                <rule name="myapp">
                    <match url="(.*)" />
                    <action type="Rewrite" url="app.js" />
                </rule>
            </rules>
        </rewrite>
    <system.webServer>
<configuration>
            

app.js is the root express file for my node application, if yours is different change it here in the web.config.

You should now be able to hit the root of the site, and if the Url Rewrite module is available all of the express routes should work also.

Conclusion

I really wanted to get this site working on Heroku, but IIS seems to be doing just fine. It took me less than 30 minutes to figure out that it was possible to host node in IIS to actually loading up upnxt.com and interacting with the site. I've contemplated giving Heroku another try, but after seeing some iisnode benchmarks and realizing that this site is going to be low volume I'm feeling pretty good with this solution.