7

I am trying to deploy a Node.js application on IIS. I saw the samples on the GitHub repository (https://github.com/tjanczuk/iisnode/tree/master/src/samples).

I am stuck at serving static files. Like a normal Node application, I stored the static files in a folder named public. As per suggestions on several blogs/forums I added the following rule to web.config:

<rule name="StaticContent">
  <action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>

But it doesn't work. If anyone has a sample application demonstrating this issue, it would be of great help.

S. Ravi Kiran
  • 4,053
  • 3
  • 21
  • 26

2 Answers2

10

In case anyone comes across this question on Google and had problems with the sample web.config mentioned in the accepted answer...

This is the web.config file that worked for me:

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
    <system.webServer>           
        <handlers>  
            <add name="iisnode" path="server/app.js" verb="*" modules="iisnode" />  
        </handlers>  
        <rewrite>  
            <rules>  
                 <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">  
                      <match url="iisnode" />  
                 </rule>  
                 <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                      
                    <match url="^server\/app.js\/debug[\/]?" />  
                 </rule>  
                <rule name="StaticContent" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url=".*" />
                    <action type="Rewrite" url="public/{C:1}" logRewrittenUrl="true" />
                    <conditions>
                        <add input="{REQUEST_URI}" pattern=".*?virtualpath\/(.*)" />
                    </conditions>
                </rule>
                    <rule name="DynamicContent" patternSyntax="ECMAScript">
                    <match url=".*" />
                    <conditions>
                        <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True" />
                    </conditions>
                    <action type="Rewrite" url="server/app.js" logRewrittenUrl="true" />
                </rule>
            </rules>  
       </rewrite>  

        <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="node_modules" />
                </hiddenSegments>
            </requestFiltering>
        </security> 
    </system.webServer>  
</configuration>

My folder structure is:

  • virtualpath/ - refers to the IIS configured Virtual Path
    • public/ - contains static content
    • server/ - contains server application files
Mark T
  • 785
  • 6
  • 13
5

Check out a sample iisnode web.config that redirects requests for static files in the public folder to the IIS's static file handler instead of Node.js at http://tomasz.janczuk.org/2012/05/yaml-configuration-support-in-iisnode.html.

Tomasz Janczuk
  • 3,220
  • 21
  • 19
  • Thanks for the answer. What should be format of the relative path to be specified while referring to the script file in an HTML file after applying the rewrite? – S. Ravi Kiran Jun 10 '14 at 19:12
  • The same as if the web.config was completely absent and default IIS rules applied (including not recognizing your server.js as a Node.js application). The static content rule does not rewrite the URL, it just prevents the iisnode handler from intercepting it. – Tomasz Janczuk Jun 10 '14 at 20:02
  • Just confirming, so relative path of a static JavaScript file would be /appname/public/scripts/main.js. Correct me if I am wrong anywhere. – S. Ravi Kiran Jun 11 '14 at 02:35
  • Just a quick note: {{REQUEST_URI}} didn't work for me, but {REQUEST_URI} worked just file. I've also changed "Static rule" to match static file, not to check if it's physical file in dynamic. So it looks pretty much as typical nginx config: and rewrite – Dmitri Sosnik Jun 25 '14 at 06:09
  • @DmitriSosnik do you have an example? – Josh C. Mar 17 '15 at 03:11
  • 2
    @JoshC. Sure, this one works for me - [https://gist.github.com/dimavs/ab540cc78b689c2727aa](https://gist.github.com/dimavs/ab540cc78b689c2727aa) – Dmitri Sosnik Mar 17 '15 at 03:21
  • 1
    Anyone mind answering why is it necessary to redirect requests to IIS's static file handler? Why can't I just use the Node/Express to serve static files? – TheCrazyProgrammer Apr 24 '17 at 21:38
  • @TheCrazyProgrammer "Using IIS static file handler has a large performance benefit compared to serving static content from within a node.js application. The handler leverages IIS and OS low level caching mechanisms which offer superb performance." I haven't run any tests but guess it makes sense. – volume one Nov 08 '19 at 11:13