Node.js 服务器发送的事件与Azure中的Windows应用程序服务不兼容
我已尝试在Azure上的Windows应用程序服务上运行以下代码:Node.js 服务器发送的事件与Azure中的Windows应用程序服务不兼容,node.js,azure,azure-web-app-service,iisnode,azure-appservice,Node.js,Azure,Azure Web App Service,Iisnode,Azure Appservice,我已尝试在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.se
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>
但是仍然没有用。您的项目可以在本地运行,也就是说,通过命令行npm start
,npm run dev
等在本地启动webapp。此时,将不使用web.config
文件。除非在本地IIS
中部署项目,IIS
将识别web.config
文件
假设您的项目没有问题,我认为您可以先删除web.config
文件(必须备份)。然后使用git进行部署,然后通过kudu找到部署自动生成的web.config
(也需要备份,因为后续操作会修改源文件,如果修改错误,可以恢复)
比较当前项目中的web.config
内容与自动生成的git部署之间的差异,并添加所需的功能和内容,例如flushResponse=“true”
,responseBufferLimit=“0”
如果出现问题,请记住恢复备份文件。这可用于故障排除
您的项目可以在本地运行,即通过命令行npm start
,npm run dev
等在本地启动webapp。此时,将不使用web.config
文件。除非在本地IIS
中部署项目,IIS
将识别web.config
文件
假设您的项目没有问题,我认为您可以先删除web.config
文件(必须备份)。然后使用git进行部署,然后通过kudu找到部署自动生成的web.config
(也需要备份,因为后续操作会修改源文件,如果修改错误,可以恢复)
比较当前项目中的web.config
内容与自动生成的git部署之间的差异,并添加所需的功能和内容,例如flushResponse=“true”
,responseBufferLimit=“0”
如果出现问题,请记住恢复备份文件。这可用于故障排除
这个问题的核心是学习如何排除故障。请参考我的建议,需要修改的属性,逐步添加并检查到web.config
文件。可能是这样,但我不知道如何进一步解决此问题。是否有调试web.config文件的方法?是否有一个服务器发送事件示例项目的工作示例,您可以向我介绍它的工作位置?此问题的核心是学习如何进行故障排除。请参考我的建议,需要修改的属性,逐步添加并检查到web.config
文件。可能是这样,但我不知道如何进一步解决此问题。是否有调试web.config文件的方法?是否有一个服务器发送事件示例项目的工作示例,您可以向我介绍它的工作位置?最初部署应用程序时,会自动生成web.config
文件,然后将其修改为如上所示。我以前将web.config文件更改为无效语法,它导致了500个错误,因此我知道它正在尝试读取它。web.config
文件是在我最初部署应用程序时自动生成的,然后我将其修改为如上所示。我以前将web.config文件更改为无效语法,它导致了500个错误,因此我知道它正在尝试读取它。