2

I am using a Rewrite rule in my web.config file for a node app running under issnode to point to my server.js file. myapp/* points to server.js.

    <rule name="myapp" enabled="true">
      <match url="myapp/*" />
      <action type="Rewrite" url="server.js" />
    </rule>

This has been working great www.mywebsite.com/myapp/ would load a run my app. What I wanted was to have a redirect from the root of the website so www.mywebsite.com/ would run my app. So I changed my web.config file

    <rule name="myapp" enabled="true">
      <match url="/*" />
      <action type="Rewrite" url="server.js" />
    </rule>

So this is running the server.js and serving up a my a static html file, the only problem is referencing any external files from my html file (css, js, images etc) Just get 500s for every request. I am using this to serve static files

        var libpath = require('path');
        var _path = "."; <-- This seems to be the problem


        var uri = url.parse(req.url).pathname;
    var filename = libpath.join(_path, uri);
    fs.readFile(filename, "binary", function (err, file) {
            if (err) {
            res.writeHead(500, {
                "Content-Type": "text/plain"
            });
                 res.write(err + "\n");
                 res.end();
                return;
            }

         var type = mime.lookup(filename);
            res.writeHead(200, {
                "Content-Type": type
            });
         res.write(file, "binary");
         res.end();
        });

    break;

So my question is how to point to root of my node app / server to serve up static files.

Thanks

Jono

Jono
  • 280
  • 1
  • 7
  • 15

2 Answers2

5

The best way to serve static content in iisnode is to configure the URL rewriting module such that the IIS static file handler handles requests for static content rather than node.js. Having IIS serve static content has a large performance benefit over serving these files using any node.js mechanisms due to kernel level optimizations around caching and just not having to break into JavaScript code.

For a boilerplate web.config configuration that achieves this see https://github.com/tjanczuk/iisnode/issues/160#issuecomment-5606547

Tomasz Janczuk
  • 3,220
  • 21
  • 19
  • 2
    I had issues with the suggested web.config and ended up with this: http://pastebin.com/hULgnCTg – GotDibbs Aug 30 '12 at 02:59
  • @GotDibbs Same for me. Tomasz web.config, produces infinite redirect loops when I go to mydomain/index.html. I just copy pasted his code. However your code works instantly. – Christiaan Westerbeek Dec 19 '14 at 16:35
  • I'm also using the boilerplate `web.config` and am not having any luck serving static files (`/public/hello.html`) – Ben Collins Dec 31 '14 at 06:15
3

I had some issues using the suggested rule config, so I made some changes:

<system.webServer>
  <handlers>
    <clear />
    <add name="iisnode" path="/index.js" verb="*" modules="iisnode" />
    <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
  </handlers>
  <rewrite>
    <rules>
      <rule name="static">
        <action type="Rewrite" url="www{REQUEST_URI}" />
      </rule>
      <rule name="serve-static" stopProcessing="true">
        <conditions logicalGrouping="MatchAny">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" />
          <add input="{REQUEST_URI}" pattern="^/www/$" />
        </conditions>
      </rule>
      <rule name="node">
        <action type="Rewrite" url="index.js" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
  • First rule prefixes all requests with www, my client directory.
  • Second rule stops processing if the file exists or if the root path is requested
  • Third rule rewrites anything else to the name of my backend Node script so that it can be picked up by the iisnode handler.

Note the iisnode handler path is set to /index.js which seems to eliminate conflicts with client files of the same name.

Chris
  • 425
  • 3
  • 10
  • Thanks, I spent a ton of time to find the solution, without having nothing works in static serving, which is missing in the Tomasz's answer and online examples – tahsintahsin Mar 19 '17 at 16:32
  • @tahsintahsin Can you post your web.config file please. I'm still having this issue with my iisnode. – ChiragMS Dec 15 '19 at 04:42
  • @ChiragMS If the file requested is under public directory, this rule stops the server from processing so that iis serves the files, it is the same as the answer above, except that the directory is different and matchany is replaced with matchall – tahsintahsin Dec 20 '19 at 08:55