M14
M14

Reputation: 1810

iisnode 401 message returns iis page

I am building an application for which i am using nodejs express to do rest api services. I hosted that application on windows server 2012 using iisnode module.Everything works perfect. The issue is that when i am returning 404(unauthorized) message from node application the end point is recieving the 401 http status with the error page of iis. Is there any other way to overcome this issue.

here is my webconfig file

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

My code in nodejs app is the following

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

enter image description here

Thanks in advance

Upvotes: 2

Views: 1448

Answers (2)

peteb
peteb

Reputation: 19428

Using IIS, if you want to return the errors straight from your Node app to the requester without IIS intercepting them with default Error Pages, use the <httpErrors> element with the existingResponse attribute set to PassThrough

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

Upvotes: 4

Tom Jardine-McNamara
Tom Jardine-McNamara

Reputation: 2618

IIS is obscuring the "detail" of the error (your json response) because by default errors are set to DetailedLocalOnly, meaning that the error detail will be shown for requests from the machine the website is running on, but show the generic IIS error page for that error code for any external requests.

Setting the 401 error to Detailed instead should allow your json response through:

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

See https://www.iis.net/configreference/system.webserver/httperrors for details.

Upvotes: 0

Related Questions