【问题标题】:Server-sent Events not working with Window App Service in Azure服务器发送的事件不适用于 Azure 中的 Window 应用服务
【发布时间】:2020-09-18 14:34:18
【问题描述】:

我尝试让以下代码在 Azure 上的 Windows 应用服务上运行:

const http = require('http');

const server = http.createServer((request, response) => {

  response.setHeader('Content-Type', 'text/event-stream');
  response.setHeader('Cache-Control', 'no-cache');
  response.setHeader('Connection', 'keep-alive');
  response.setHeader('Transfer-Encoding', 'chunked');
  response.flushHeaders();

  var interval = setInterval(function () {
      response.write("data: extra data\n\n");
  }, 1000);

  request.on('close', function () {
    clearInterval(interval);
  })
});

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

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

当我在本地运行它时它可以工作,但在部署到应用服务时就不行。

我的web.config 如下:

<?xml version="1.0" encoding="utf-8"?>
<!--
     This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:

     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->

<configuration>
  <system.webServer>
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <webSocket enabled="false" />
    <handlers>
      <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" responseBufferLimit="0"/>
    </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{PATH_INFO}"/>
        </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" />

    <!--
      You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled

      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
    -->
    <!--<iisnode watchedFiles="web.config;*.js"/>-->
    <iisnode flushResponse="true" />
  </system.webServer>
</configuration>

我已根据 Microsoft 应用服务文档的建议添加了 &lt;iisnode flushResponse="true" /&gt;responseBufferLimit="0"https://docs.microsoft.com/en-us/azure/app-service/app-service-web-nodejs-best-practices-and-troubleshoot-guide#flushresponse,但仍然无济于事。

【问题讨论】:

  • 这个问题的核心是学会排查。请参考我的建议,需要修改的属性,逐步添加到web.config文件中检查。
  • 很可能是这种情况,但我不知道如何进一步解决此问题。有没有办法调试 web.config 文件?是否有服务器发送事件示例项目的工作示例,您可以向我推荐它在哪里工作?

标签: node.js azure azure-web-app-service iisnode azure-appservice


【解决方案1】:

你的项目可以在本地运行,即通过命令行npm startnpm run dev等在本地启动webapp。此时,web.config 文件将不会被使用。除非你在本地IIS部署项目,否则IIS会识别web.config文件。

假设你的项目没有问题,我想你可以先删除你的web.config文件(一定要备份)。然后使用git部署,然后通过kudu,找到部署自动生成的web.config(也需要备份,因为后续操作会修改源文件,如果修改错误可以恢复)。

A post about git deployment to automatically generate web.config.

  1. 对比一下你当前项目中web.config的内容和自动生成的git部署的区别,添加你想要的功能和内容,比如flushResponse="true",responseBufferLimit="0"

  2. 如果出现问题,请记住恢复备份文件。这可用于故障排除。

【讨论】:

  • web.config 文件是我最初部署应用程序时自动生成的,然后我将其修改为如上所示。我之前已将 web.config 文件更改为无效语法,并导致 500 错误,所以我知道它正在尝试读取它。
猜你喜欢
  • 2014-04-10
  • 1970-01-01
  • 2015-06-23
  • 2020-02-19
  • 2020-11-04
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多