Replace res.write in NodeJS

I played with NodeJS for a while, and I really enjoyed it, but I threw myself into the wall, though ...

I am trying to create a plug-in to intercept http.ServerResponse methods. My ultimate goal is to allow the user to apply some sort of filter to the outgoing data. For example, they will have the opportunity to apply compression or something else before the data disappears.

I have this strange error, though ... this method is called twice more often than it should be.

Here is 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);

, "Hello World" , , 4 "Called" , "Done" . , - . console.log newWrite this.constructor, - ServerResponse, .

, . ? , , , , .

, , .

Edit:

, :

http.ServerResponse node.js?

+3
2

: console.log(req.url) createServer, , , , . url /favicon.ico, html- favicon.

+6

All Articles