开发者

Override res.write in NodeJS

开发者 https://www.devze.com 2023-02-17 21:05 出处:网络
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'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 ap开发者_开发百科ply 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:

Can I use an http.ServerResponse as a prototype in node.js?


There is no problem with your code: if you add a console.log(req.url) in the createServer 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.


You can use connect's favicon middleware to avoid that problem:

http://senchalabs.github.com/connect/middleware-favicon.html

0

精彩评论

暂无评论...
验证码 换一张
取 消