Tuesday, July 29, 2014

IIS Node for first timers

Target Audience

This how-to blog is intended for individuals that are comfortable creating sites in IIS (possibly including setting up SSL certificates) and are now looking to run a Node website.


Background

If you're interested, the next paragraph describes my requirements when I went through this process.  Feel free to skip to the steps.

We've been working on an internal coding competition site for the past few months now and we chose to strictly use Node rather than making it a .Net website.  It's being hosted using an Azure VM because we also needed a local MongoDB to house the data.  We also require https since we're providing user auth.


Step 1

On your server (or machine) make sure to install the necessary software:

1. Node
2. IIS rewrite module
3. IISNode for IIS 7.x/8.x: x86 or x64


Step 2

Create an IIS application which is pointing to the root folder of your Node app.


Step 3

In your Node code, make sure you use the following variable for the port that Express (or whatever web server you're using) listens on.
var port = process.env.PORT;


Step 4

Add a web.config file to the root folder of your Node app and paste the following code in it.  Note that there is a section to force SSL, if you don't need this then comment that section out or remove it.
<configuration>
  <system.webServer>

    <handlers>
      <!-- Change server.js to the starting node script of your app -->
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
      <rules>

        <!-- Force SSL -->
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
            <conditions>
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            </conditions>
          <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
        </rule>

        <!-- Catch any url the user is trying to go to and go directly to the starting node script -->
        <rule name="server">
           <match url="/*" />
           <action type="Rewrite" url="server.js" />
        </rule>

      </rules>
    </rewrite>

    <!-- Path to node on the local machine -->
    <iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />

  </system.webServer>
</configuration>

That should be all the steps needed.  Your node website should come up without the need to fire up node in any console.

No comments:

Post a Comment