为什么Azure服务器响应错误请求?

时间:2017-01-06 10:17:25

标签: javascript node.js azure npm

我在Azure服务器上运行我的NodeJS应用程序,所有API都正常运行并且响应正确。

现在的问题是,当我运行这种GET API时:

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

所需的NodeJS代码是:

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

每当请求成功时,它会给出所需输出的正确响应,现在问题是当请求失败时发现的错误没有给我一个错误消息作为响应,它总是给我Bad Request 400代码,

我该怎么做才能处理错误响应?

1 个答案:

答案 0 :(得分:1)

我可以使用以下代码行重现这一点。

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

enter image description here

要解决此问题,我们需要将以下标记添加到web.config文件中。

<httpErrors existingResponse="PassThrough" />

web.config的完整档案如下所示:

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

之后,您将收到错误消息作为回应。

enter image description here