【问题标题】:Why is Azure server responding with Bad Request?为什么 Azure 服务器响应错误请求?
【发布时间】:2017-01-09 11:23:35
【问题描述】:

我在 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 代码,

这里应该怎么处理错误响应?

【问题讨论】:

    标签: javascript node.js azure npm


    【解决方案1】:

    我可以用下面的代码行重现这个。

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

    要解决这个问题,我们需要在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>
    

    然后,您将收到错误消息作为响应。

    【讨论】:

    • 哇,谢谢,我已经更改和验证我的代码太多次了,非常感谢亲爱的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 2019-07-26
    • 1970-01-01
    相关资源
    最近更新 更多