Node.js 当节点应用程序返回4xx时,IIS返回500

Node.js 当节点应用程序返回4xx时,IIS返回500,node.js,azure,iis,iisnode,Node.js,Azure,Iis,Iisnode,我也在网上问过这个问题 我正在通过Windows Azure网站使用IISNode 如果我的节点应用程序返回一个2xx状态代码(200、201等),那么一切正常,并按预期工作 如果我的节点应用程序返回4xx状态代码,例如: response.send(404,“未找到”)(我使用的是Restify),然后我收到一个500发送到客户端,正文只是“由于发生内部服务器错误,无法显示页面。” 如果我做了azure站点日志尾,那么当500被发送到客户端时,日志包含404的HTML ... <body

我也在网上问过这个问题

我正在通过Windows Azure网站使用IISNode

如果我的节点应用程序返回一个2xx状态代码(200、201等),那么一切正常,并按预期工作

如果我的节点应用程序返回4xx状态代码,例如:
response.send(404,“未找到”)
(我使用的是Restify),然后我收到一个500发送到客户端,正文只是“由于发生内部服务器错误,无法显示页面。”

如果我做了azure站点日志尾,那么当500被发送到客户端时,日志包含404的HTML

...
<body> 
<div id="content"> 
<div class="content-container"> 
<h3>HTTP Error 404.0 - Not Found</h3> 
<h4>The resource you are looking for has been removed, had its name changed,
or is temporarily unavailable.</h4> 
</div> 
...
。。。
HTTP错误404.0-找不到
您正在查找的资源已被删除,名称已更改,
或暂时不可用。
...
我真的只想IISNode/IIS将我的404发送到客户端。401、409等也是如此。它们都会导致发送500

我在我的web.config中尝试了
,但没有成功。下面是我的web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <customErrors mode="off" />
  </system.web>
  <system.webServer>         
    <httpErrors existingResponse="PassThrough" />
    <staticContent>
       <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
       <mimeMap fileExtension=".ico" mimeType="image/x-icon" />
    </staticContent>
    <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"/>
           <match url="/*" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>


这正是我的问题。除了
对我不起作用。我觉得我的IISNode设置有更根本的问题,所以如果有人有任何想法可以尝试,我会洗耳恭听。

出于某种原因,您的配置中的以下部分会导致此问题:

<staticContent>
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
    <mimeMap fileExtension=".ico" mimeType="image/x-icon" />
</staticContent>


作为一种解决方法,如果可能,请删除此部分,然后重试。我将调查此部分导致此问题的原因。

我从Azure/IIS返回了完全相同的…500错误页面。我使用Azure自动生成的web.config作为基础,然后添加了
。我还删除了默认的staticContent节点。什么都没用

下面是我正在上传到Azure的web.config。这里是什么导致IIS/iisnode返回500错误页面,而不是我让node/express返回到客户端的简单文本错误消息

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <customErrors mode="off" />
  </system.web>
  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
    <webSocket enabled="false" />
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^server.js\/debug[\/]?" />
        </rule>
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="server.js"/>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

这对我很有用:

<?xml version="1.0" encoding="utf-8"?> 
  <configuration>
    <system.webServer>
      <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>

正如Ranjith指出的那样,
staticContent
部分是冲突的原因。我刚刚发现,您可以将这些条目限制在
location
条目中,这样您就可以根据需要获取所有IISNode,并且仍然提供静态内容。我的web.config现在如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <customErrors mode="off" />
  </system.web>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="Redirect to https" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" appendQueryString="true" />
        </rule>

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

        <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>

 <location path="public/fonts">
    <system.webServer>
      <staticContent>
         <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>

最后添加的
位置
条目解决了500和静态内容冲突。

我也遇到了同样的问题(Win Server 2012R2上的IIS节点)。因此,我没有得到身体中的目标内容

一开始我得到的回应是:

code: 400
body: Bad Request
code: 500
body: The page cannot be displayed because an internal server error has occurred
code: 400
body: { "error": {...} }
然后我添加了
,得到了响应:

code: 400
body: Bad Request
code: 500
body: The page cannot be displayed because an internal server error has occurred
code: 400
body: { "error": {...} }
然后,我将“错误页面”的IIS功能委派更改为“读/写”,这解决了我的问题。最后我得到了恰当的回应:

code: 400
body: Bad Request
code: 500
body: The page cannot be displayed because an internal server error has occurred
code: 400
body: { "error": {...} }

我希望这能有所帮助。

我有一个IISNode应用程序运行时没有问题,我的web.config和你的web.config之间唯一的主要区别是我没有
DynamicContent
中的
规则,删除它对你有什么影响吗?我也没有
httpErrors
部分。我取出了匹配标签,这没有改变任何东西。我还尝试创建一个新的网站,并将其部署到该网站上,但效果相同。我计划在本周末尝试一些其他东西。如果我找到一个解决方案,我会在这里更新(和github),我会在今晚回家的时候试一试。不过,我不知道没有这些mimemaps该怎么办。这确实有效,而且我似乎不再需要
staticContent
。我不知道这是为什么。但既然一切都在运转,现在就不用再多问了:)谢谢兰吉思!不适合我。在节点中,我使用express来
res.send(400,“这是我的错误”)。Azure/IIS正在将其转换为标准IIS 500错误页。您是否尝试删除NodeInspector规则?我从来没有遇到过这样的运气。嗨,埃里克,我尝试了你的web.config和下面的server.js,我得到了预期的错误代码,即409。Server.js:----------------------------var http=require('http');http.createServer(函数(req,res){res.writeHead(409,{'Content-Type':'text/html'});res.end('Hello,world!');})如果你可以共享一个简单的复制应用程序,我将进一步调查。这就成功了