在 NodeJS 中重写 res.write
我已经使用 NodeJS 一段时间了,我真的很喜欢它,但我已经碰壁了......
我正在尝试创建一个连接模块来拦截 http.ServerResponse 方法。我的最终目标是允许用户对传出数据应用某种过滤器。例如,他们可以选择在数据流出之前应用压缩或其他操作。
不过,我遇到了这个奇怪的错误...这个方法被调用的频率是应有的两倍。
这是我的代码:
var http = require('http'), orig;
orig = http.ServerResponse.prototype.write;
function newWrite (chunk) {
console.log("Called");
orig.call(this, chunk);
}
http.ServerResponse.prototype.write = newWrite;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("Hello");
res.write(" World");
res.end();
console.log("Done");
}).listen(12345);
这有效,当我使用浏览器访问它时,我得到“Hello World”作为输出,但是我在控制台上得到了 4 个“Called”,并且“Done”得到了两次输出。这让我相信由于某种原因,我的服务器代码被调用了两次。我在 this.constructor 上的 newWrite 方法中做了一个 console.log ,并且两个实例中的构造函数都是 ServerResponse ,所以这没有多大帮助。
我真的很困惑这里发生了什么。可能发生了什么?这不会直接影响输出,但我可能会同时向许多客户端提供千兆字节的压缩数据,并且执行两次操作会给我的服务器带来过度的压力。
这将成为更大文件服务器的一部分,因此强调不要将所有事情都做两次。
编辑:
如果您想知道,我已经读过这个问题:
I've been playing with NodeJS for a while now, and I've really been enjoying it, but I've run myself into a wall though...
I'm trying to create a connect module to intercept http.ServerResponse methods. My end goal is to allow the user to apply some kind of filter to outgoing data. For example, they would have the option to apply compression or something before the data goes out.
I am having this weird bug though... this method is getting called twice as often as it should be.
Here's my code:
var http = require('http'), orig;
orig = http.ServerResponse.prototype.write;
function newWrite (chunk) {
console.log("Called");
orig.call(this, chunk);
}
http.ServerResponse.prototype.write = newWrite;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("Hello");
res.write(" World");
res.end();
console.log("Done");
}).listen(12345);
This works and I get 'Hello World' as output when I access this using a browser, but I'm getting 4 'Called' to the console, and 'Done' gets output twice. This leads me to believe that for some reason, my server code is getting called twice. I did a console.log in my newWrite method on this.constructor, and the constructor in both instances is ServerResponse, so that doesn't help much.
I'm really confused about what is going on here. What could possibly be going on?? This doesn't directly affect the output, but I could potentially be serving gigabytes of compressed data to many clients simultaneously, and doing everything twice will put undue strain on my server.
This is going to be part of a larger fileserver, hence the emphasis on not doing everything twice.
Edit:
I've already read this question in case you're wondering:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码没有问题:如果您在
createServer
调用中添加console.log(req.url)
,您可能会看到它实际上被调用了两次通过您的浏览器。如果 html 标记中未指定 favicon,大多数浏览器都会对所请求的 url 发出请求,并附加调用/favicon.ico
。There is no problem with your code: if you add a
console.log(req.url)
in thecreateServer
call, you'll probably see that it is actually called twice by your browser. Most browsers make a request for the requested url and an additional call for/favicon.ico
if no favicon is specified in the html markup.您可以使用 connect 的 favicon 中间件来避免该问题:
http://senchalabs.github.com/连接/middleware-favicon.html
You can use connect's favicon middleware to avoid that problem:
http://senchalabs.github.com/connect/middleware-favicon.html