Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static files in IISNODE with StaticFiles URL Rewrite

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.

like image 839
S. Ravi Kiran Avatar asked Jun 08 '14 08:06

S. Ravi Kiran


2 Answers

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
like image 97
Mark T Avatar answered Oct 07 '22 13:10

Mark T


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.

like image 41
Tomasz Janczuk Avatar answered Oct 07 '22 13:10

Tomasz Janczuk