iisnode 401消息返回iis页面

我正在build立一个应用程序,我使用nodejs express来完成其他api服务。 我使用iisnode模块在windows server 2012上托pipe了这个应用程序。一切都很完美。 问题是,当我从节点应用程序返回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(); } 

在这里输入图像描述

提前致谢

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,如果要直接从节点应用程序将错误返回给请求方,而不用IIS使用默认的错误页面拦截它们,请使用<httpErrors>元素,并将existingResponse属性设置为PassThrough

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