2016-10-20 37 views
2

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

这里是我的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代码下面的advanc

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

enter image description here

谢谢e

回答

0

由于默认错误设置为DetailedLocalOnly,这意味着将显示错误详细信息,以显示网站运行的计算机的请求的错误详细信息,但会显示错误的详细信息(您的json响应)针对任何外部请求的错误代码的通用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

+0

你好Tom 。谢谢你的支持。,我试过上面的那个。但是我得到了内部服务器错误 – M14

+0

嗯,我目前没有访问我的iis服务器。我从我链接的文档页面调整了这个例子,所以也许有一个通读的例子 –

0

使用IIS,如果你想从您的节点的应用程序直接返回错误给请求者,而不IIS拦截他们默认的错误页面,使用<httpErrors>元素与existingResponse属性设置为PassThrough

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