2016-10-20 35 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>