首页 > 解决方案 > 部署在 Azure 中的 nodejs 服务器正在输出内容长度

问题描述

我的server.js样子是这样的:

var http = require('http');

var server = http.createServer(function (request, response) {

    const configs = {
        apiBaseUrl: 'http://myUrl'
    };

    const headers = {
        'Content-Type': 'application/json'
    };
    response.writeHead(200, headers);
    response.end(JSON.stringify(configs));
});

var port = process.env.PORT || 1337;
server.listen(port);

console.log("Server running at http://localhost:%d", port);

当我在本地运行它时,它会输出它应该输出的内容:

{"apiBaseUrl":"http://myUrl"}

但是当部署到我的 Azure 实例时,它的行为略有不同。它还输出字符数:

29
{"apiBaseUrl":"http://myUrl"}
0

任何线索可能导致此问题?

编辑: 我忘了提到我正在部署到 Azure 并有一个web.config到位:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Get dynamic configs in server environment" stopProcessing="true">
                    <match url="configs.json" ignoreCase="true"/>
                    <action type="Redirect" url="currentConfigs.js" redirectType="Permanent" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
        <handlers>
            <add name="iisnode" path="currentConfigs.js" verb="*" modules="iisnode" />
        </handlers>
    </system.webServer>
</configuration>

标签: node.jsazureiis

解决方案


我做了以下观察:

在 Azure 门户中,我通过单击按钮打开了 Application Insights 站点扩展。

在此处输入图像描述

这会自动创建以下设置:

  • APPINSIGHTS_INSTRUMENTATIONKEY
  • APPINSIGHTS_PROFILERFEATURE_VERSION:1.0.0
  • APPINSIGHTS_SNAPSHOTFEATURE_VERSION:1.0.0
  • ApplicationInsightsAgent_EXTENSION_VERSION:~2
  • 诊断服务_EXTENSION_VERSION:~3
  • InstrumentationEngine_EXTENSION_VERSION:~1
  • SnapshotDebugger_EXTENSION_VERSION:~1
  • XDT_MicrosoftApplicationInsights_BaseExtensions:~1
  • XDT_MicrosoftApplicationInsights_Mode:推荐

我的节点请求的响应现在是:

29
{"apiBaseUrl":"http://myUrl"}
0

在玩弄了这些新值之后,我发现了导致问题的值:

  • ApplicationInsightsAgent_EXTENSION_VERSION:~2

将其更改为 ~1 解决了该问题,并且响应看起来符合预期:

  • ApplicationInsightsAgent_EXTENSION_VERSION:~1

回复:

{"apiBaseUrl":"http://myUrl"}

我认为这是一种超级奇怪的行为。我将创建一个 github 问题并链接它。


推荐阅读