Jondlm
Jondlm

Reputation: 8892

Node.js on Azure Websites 404 Error

I am running a Node.js 0.10.15 & Express app on Azure Websites and am having a problem sending 404s when someone enters an invalid URL. I am getting the generic The page cannot be displayed because an internal server error has occurred. error when I attempt to access an invalid URL.

Here is the code at the end my app.js file that sends the 404 page:

app.get('/*', function(req, res) { res.status(404).sendfile('public/404.html'); });

The strange bit is that Express seems to be properly sending the 404 along with the HTML file when I run my server locally, but when it's running on Azure it chokes and doesn't give me a useful error. The logs make it appear that the request worked as expected:

[Thu, 24 Oct 2013 01:10:39 GMT] "GET /asdfsadf HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"

I suspect the problem somehow lies in my web.config file:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <system.webServer>
      <httpErrors existingResponse="PassThrough" />
      <staticContent>
         <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
         <mimeMap fileExtension=".woff" mimeType="application/x-woff" />
         <mimeMap fileExtension=".ttf" mimeType="application/x-woff" />
      </staticContent>
      <handlers>
        <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
      </handlers>
      <rewrite>
        <rules>
            <rule name="DynamicContent">
                 <match url="/*" />
                 <action type="Rewrite" url="app.js"/>
            </rule>
       </rules>
      </rewrite>
    </system.webServer>
  </configuration>

I also have logging turned on with the iisnode.yml file:

loggingEnabled: true
devErrorsEnabled: true

Also, you can see my dev working branch over at GitHub

Upvotes: 3

Views: 2178

Answers (2)

BotanMan
BotanMan

Reputation: 1417

When I met this issue that help me

<staticContent>
    <remove fileExtension=".svg" />
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />

    <remove fileExtension=".woff" />
    <mimeMap fileExtension=".woff" mimeType="application/application/font-woff" />

    <remove fileExtension=".ttf" />
    <mimeMap fileExtension=".ttf" mimeType="application/x-font-truetype" />

    <remove fileExtension=".otf" />
    <mimeMap fileExtension=".otf" mimeType="application/x-font-opentype" />
</staticContent>

So before declaring a mimeMap it's necessary to be sure that mimeType not registered by default, and that's why you can remove mimeType and then register it.

Upvotes: 0

sebastiandurandeu
sebastiandurandeu

Reputation: 261

See Windows Azure Websites is overriding my 404 and 500 error pages in my node.js app and https://github.com/tjanczuk/iisnode/issues/295

Both worked for me. I did not had any Web.config for my express application and this is what I used:

<configuration>
    <system.webServer>
      <handlers>
        <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
      </handlers>
      <rewrite>
           <rules>
                <rule name="DynamicContent">
                     <conditions>
                          <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True"/>
                     </conditions>
                     <action type="Rewrite" url="server.js"/>
                </rule>
           </rules>
      </rewrite>
      <httpErrors existingResponse="PassThrough"/>
    </system.webServer>
</configuration>

Upvotes: 3

Related Questions