1
votes

I've tried to get the following code running on a Windows App Service on Azure:

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);

It works when I run it locally, however does not when deployed to the App Service.

My web.config looks like as follows:

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

I've added in <iisnode flushResponse="true" /> and responseBufferLimit="0" as per suggestions from the Microsoft App Service docs: https://docs.microsoft.com/en-us/azure/app-service/app-service-web-nodejs-best-practices-and-troubleshoot-guide#flushresponse but still to no avail.

1
The core of this problem is to learn to troubleshoot. Please refer to my suggestion, the attributes that need to be modified, add and check step by step to the web.config file.Jason Pan
That may well be the case but I don't know how to troubleshoot this issue any further. Is there a method to debug the web.config file? Is there a working example of Server-sent events example project you can refer me to where this is working?rndware

1 Answers

0
votes

Your project can run locally, that is, through the command line npm start, npm run dev, etc. to start webapp in local. At this time, the web.config file will not be used. Unless you deploy the project in the local IIS, IIS will recognize the web.config file.

Assuming that there is no problem with your project, I think you can delete your web.config file first (must be backed up). Then use git to deploy, and then through kudu, find the web.config automatically generated by the deployment (also needs to be backed up, because subsequent operations will modify the source file, if the modification is wrong, you can restore it).

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

  1. Compare the difference between the web.config content in your current project and the automatically generated git deployment, and add the functions and content you want, such as flushResponse="true",responseBufferLimit="0".

  2. If something goes wrong, remember to restore the backup file. This can be used for troubleshooting.