node.js - 服务器发送的事件不适用于 Azure 中的 Window 应用服务
问题描述
我试图让以下代码在 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>
我已经添加<iisnode flushResponse="true" />
并responseBufferLimit="0"
根据 Microsoft App Service 文档的建议:https ://docs.microsoft.com/en-us/azure/app-service/app-service-web-nodejs-best-practices-and-故障排除指南#flushresponse但仍然无济于事。
解决方案
你的项目可以在本地运行,即通过命令行npm start
、npm run dev
等方式在本地启动webapp。此时,该web.config
文件将不会被使用。除非你在本地部署项目,否则IIS
会IIS
识别web.config
文件。
假设您的项目没有问题,我认为您可以web.config
先删除您的文件(必须备份)。然后使用git部署,然后通过kudu,找到web.config
部署自动生成的(也需要备份,因为后续操作会修改源文件,如果修改错误,可以恢复)。
一篇关于 git 部署自动生成 web.config 的帖子。
对比一下你当前项目中的内容和自动生成的git部署的区别
web.config
,添加你想要的功能和内容,比如flushResponse="true"
,responseBufferLimit="0"
.如果出现问题,请记住恢复备份文件。这可用于故障排除。
推荐阅读
- c - C - fwrite() 没有从网络缓冲区写入第一批数据
- python - 如何在python中找到给定数字的除数并将它们附加到列表中?
- c# - EF Core 通过完全替换断开了集合导航属性的更新
- c# - 为什么使用 NCalc 和字典后一个编程问题的运行时间大大增加?
- spring - 在本地主机上完美运行 AWS 上的模板解析器问题
- android - 滚动问题 - 在 NestedScrollView 中使用两个 RecyclerView
- python - 将返回值从协程传递给回调函数而不使用等待
- javascript - Typescript 中有多少类型声明才足够?
- node.js - Binance 平仓 coin-m 期货头寸 (dapi/v1)
- npm - npm 包中的 Twilio 视频持续时间和/或结束时间