我正在 Azure 网站上运行 Node.js 0.10.15 和 Express 应用程序,当有人输入无效 URL 时,我在发送 404 时遇到问题。当我尝试访问无效 URL 时,我收到通用 由于发生内部服务器错误,无法显示页面。
错误。
以下是我的 app.js
文件末尾发送 404 页面的代码:
app.get('/*', function(req, res) { res.status(404).sendfile('public/404.html'); });
奇怪的是,当我在本地运行服务器时,Express 似乎可以正确发送 404 和 HTML 文件,但是当它在 Azure 上运行时,它会阻塞并且不会给我一个有用的错误。日志显示该请求按预期工作:
[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"
我怀疑问题出在我的 web.config
文件中:
<?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>
我还使用 iisnode.yml
文件打开了日志记录:
loggingEnabled: true
devErrorsEnabled: true
此外,您可以在 GitHub 看到我的开发工作分支
最佳答案
参见Windows Azure Websites is overriding my 404 and 500 error pages in my node.js app和 https://github.com/tjanczuk/iisnode/issues/295
两者都为我工作。我的 Express 应用程序没有任何 Web.config,这就是我使用的:
<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>
关于Azure 网站上的 Node.js 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19555187/