【问题标题】:nodejs server deployed in Azure is outputting content length部署在 Azure 中的 nodejs 服务器正在输出内容长度
【发布时间】:2019-04-16 20:06:11
【问题描述】:

我的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 JS Empty Web App”启动了一个应用服务,复制并粘贴了您的代码,并收到了预期的仅 JSON 输出,所以我不确定为什么会这样正在为你发生。您如何在 Azure 中运行代码?
  • 嗨@MitchStewart我有一个使用iisnode处理程序的web.config。忘了提。谢谢指出!

标签: node.js azure iis


【解决方案1】:

我做了以下观察:

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

这会自动创建以下设置:

  • APPINSIGHTS_INSTRUMENTATIONKEY
  • APPINSIGHTS_PROFILERFEATURE_VERSION:1.0.0
  • APPINSIGHTS_SNAPSHOTFEATURE_VERSION:1.0.0
  • ApplicationInsightsAgent_EXTENSION_VERSION:~2
  • DiagnosticServices_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 问题并链接它。

【讨论】:

  • 这看起来很奇怪。你有没有创建过 Github 问题?
  • 还没有。将在接下来的几天内完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2018-04-18
  • 2019-03-09
  • 2021-12-02
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
相关资源
最近更新 更多