urlrewrite

This sample demonstrates the use of IIS URL rewrite module to redirect an entire branch of the URL namespace to a single node.js application. This mechanism allows more control over the URL structure of a node.js web service as well as removes the *.js file name from the URL.

visit the endpoint at myapp/foo/bar/baz
visit the endpoint at myapp/1/2/3?param=4
debug the hello.js application at hello.js/debug (requires WebKit enabled browser)

code

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('Hello from urlrewrite sample. Request path: ' + req.url);
}).listen(process.env.PORT);

web.config

<configuration>
  <system.webServer>

    <!-- indicates that the hello.js file is a node.js application 
    to be handled by the iisnode module -->

    <handlers>
      <add name="iisnode" path="hello.js" verb="*" modules="iisnode" />
    </handlers>

    <!-- use URL rewriting to redirect the entire branch of the URL namespace
    to hello.js node.js application; for example, the following URLs will 
    all be handled by hello.js:
    
        http://localhost/node/urlrewrite/myapp
        http://localhost/node/urlrewrite/myapp/foo
        http://localhost/node/urlrewrite/myapp/foo/bar/baz?param=bat
        
    -->
    
    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="myapp/*" />
          <action type="Rewrite" url="hello.js" />
        </rule>
      </rules>
    </rewrite>

  </system.webServer>
</configuration>