Dr. Piyush Dholariya
Dr. Piyush Dholariya

Reputation: 1326

Why is Azure server responding with Bad Request?

I am running my NodeJS application on Azure server, All APIs are working fine with proper response.

Now the problem is that when I running this kind of GET API:

https://demoapp.azurewebsites.net/mymenu?userId=piyush.dholariya

The required NodeJS code is:

app.get('/mymenu', function(req, res) {
   res.status(code).send(err || result);
});

Whenever the request is successful it gives a proper response with the required output, now the problem is whenever a request fails the found error is not giving me an error message in response, it always gives me Bad Request with 400 code,

What should I do to handle error response here?

Upvotes: 1

Views: 471

Answers (1)

Aaron Chen
Aaron Chen

Reputation: 9940

I'm able to reproduce this with the following lines of code.

app.get('/mymenu', function(req, res) {
   res.status(400).send('Something wrong');
});

enter image description here

To fix this, we need to add the following tag to the web.config file.

<httpErrors existingResponse="PassThrough" />

The full file of web.config will look something like:

<?xml version="1.0" encoding="utf-8"?>

<configuration>
     <system.webServer>

          <webSocket enabled="false" />
          <handlers>
               <!-- Indicates that the app.js file is a node.js site to be handled by the iisnode module -->
               <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
          </handlers>
          <rewrite>
               <rules>
                    <!-- Do not interfere with requests for node-inspector debugging -->
                    <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                        <match url="^app.js\/debug[\/]?" />
                    </rule>

                    <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
                    <rule name="StaticContent">
                         <action type="Rewrite" url="public{REQUEST_URI}"/>
                    </rule>

                    <!-- All other URLs are mapped to the node.js site entry point -->
                    <rule name="DynamicContent">
                         <conditions>
                              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                         </conditions>
                         <action type="Rewrite" url="app.js"/>
                    </rule>
               </rules>
          </rewrite>

          <!-- bin directory has no special meaning in node.js and apps can be placed in it -->
          <security>
               <requestFiltering>
                    <hiddenSegments>
                         <remove segment="bin"/>
                    </hiddenSegments>
               </requestFiltering>
          </security>

          <!-- Make sure error responses are left untouched -->
          <httpErrors existingResponse="PassThrough" />

          <iisnode watchedFiles="web.config;*.js" debuggingEnabled="false" />
     </system.webServer>
</configuration>

And after that, you will get the error message in response.

enter image description here

Upvotes: 1

Related Questions