Thursday 1 June 2017

Custom static error page with node and Azure WebApp

I'm building a mix of static pages and node website using Azure WebApps and I'd like to have a custom 404 page and I just can't make it work. Most of the website is static but I have a couple of routes that require server code. My web.config looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>      
        <httpProtocol>
            <customHeaders>
                <remove name="X-Powered-By"/>
                <add name="x-dns-prefetch-control" value="on"/>
            </customHeaders>
        </httpProtocol>     
        <handlers>      
            <add name="iisnode" path="src/server/index.js" verb="*" modules="iisnode"/>
        </handlers>
        <rewrite>
            <rules>
            <rule name="static">
                <match url="(?!dynamicroute).*$" ignoreCase="true"/>
                <action type="Rewrite" url="dist{REQUEST_URI}"/>
            </rule>
            <rule name="dynamic">
                <match url="(?:dynamicroute)(.*)$" ignoreCase="true"/>
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                </conditions>
                <action type="Rewrite" url="src/server/index.js"/>
            </rule>
            </rules>            
        </rewrite>
        <!-- Make sure error responses are left untouched -->
        <!--<httpErrors existingResponse="PassThrough"/>-->
        <httpErrors errorMode="Custom" defaultResponseMode="File" existingResponse="PassThrough">   
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" path="~/404.html" responseMode="File" />
        </httpErrors>
    </system.webServer>
</configuration>

I've tried with different variations of httpErrors, value of path, responseMode, removing existingResponse="PassThrough", etc. but I just can't make it work.

Accessing directly /404.html works. The server returns a 404 error when accessing a url that doesn't exist, just not the custom one I want. What am I doing wrong?

I know I could handle everything from nodeland but I'd like to avoid that if possible.



via Antón Molleda

No comments:

Post a Comment