【问题标题】:iisnode 401 message returns iis pageiisnode 401 消息返回 iis 页面
【发布时间】:2016-10-20 06:38:33
【问题描述】:

我正在构建一个应用程序,我正在使用 nodejs express 来执行 rest api 服务。 我使用 iisnode 模块在 Windows Server 2012 上托管了该应用程序。一切都很完美。 问题是当我从节点应用程序返回 404(未授权)消息时,端点正在接收带有 iis 错误页面的 401 http 状态。 有没有其他方法可以解决这个问题。

这是我的 webconfig 文件

<!-- indicates that the hello.js file is a node.js application 
to be handled by the iisnode module -->

<handlers>
  <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>

<!-- use URL rewriting to redirect the entire branch of the URL namespace
to hello.js node.js application; for example, the following URLs will 
all be handled by hello.js:
    http://localhost/node/express/myapp/foo
    http://localhost/node/express/myapp/bar
-->
<rewrite>
  <rules>
    <rule name="/">
      <match url="/*" />
      <action type="Rewrite" url="app.js" />
    </rule>
  </rules>
</rewrite>

我在 nodejs 应用中的代码如下

 if(clienttoken!=servertoken)
{
    res.json(401, 'unauthorized');
    res.end();
}

提前致谢

【问题讨论】:

    标签: asp.net node.js express iis iisnode


    【解决方案1】:

    使用 IIS,如果您想将错误直接从 Node 应用程序返回给请求者,而不需要 IIS 使用默认错误页面拦截它们,请使用 &lt;httpErrors&gt; 元素并将 existingResponse 属性设置为 PassThrough

    <configuration>
      <system.webServer>
        <httpErrors existingResponse="PassThrough" />
      </system.webServer>
    </configuration>
    

    【讨论】:

      【解决方案2】:

      IIS 掩盖了错误的“详细信息”(您的 json 响应),因为默认情况下错误设置为 DetailedLocalOnly,这意味着错误详细信息将显示来自运行网站的机器的请求,但显示任何外部请求的该错误代码的通用 IIS 错误页面。

      将 401 错误设置为 Detailed 应该允许您的 json 响应通过:

      <configuration>
        <system.webServer>
          <httpErrors errorMode="DetailedLocalOnly">
            <remove statusCode="401" />
            <error statusCode="401" errorMode="Detailed"/>
          </httpErrors>
        </system.webServer>
      </configuration>
      

      详情请见https://www.iis.net/configreference/system.webserver/httperrors

      【讨论】:

      • 嗨,汤姆。感谢您的支持。,我尝试了上面的。但是我收到内部服务器错误
      • 嗯,我目前无法访问我的 iis 服务器。我从我链接的文档页面中改编了那个例子,所以也许可以通读一下
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      相关资源
      最近更新 更多