Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override res.write in NodeJS

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:

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

like image 575
beatgammit Avatar asked Feb 24 '23 18:02

beatgammit


2 Answers

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.

like image 111
Adrien Avatar answered Mar 07 '23 01:03

Adrien


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

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

like image 30
masylum Avatar answered Mar 07 '23 03:03

masylum