Skip to content

Instantly share code, notes, and snippets.

@schamane
Created February 10, 2011 09:51
Show Gist options
  • Select an option

  • Save schamane/820216 to your computer and use it in GitHub Desktop.

Select an option

Save schamane/820216 to your computer and use it in GitHub Desktop.
Overwrite http client for nodejs
#!/bin/env node
var util = require('util'),
http = require('http');
var Foo = function () {
Foo.super_.apply(this, arguments);
this._name = 'foo';
};
util.inherits(Foo, http.Client);
//overwrite request method from superclass
Foo.prototype.request = function () {
//call superclass
var request = Foo.super_.prototype.request.apply(this, arguments);
//subscribe response
request.on('response', this._onResponse.bind(this));
return request;
};
Foo.prototype._onResponse = function(response) {
console.log('Client ' + this._name + ' responsed');
};
var t = new Foo();
t.port = 80;
t.host = "www.google.com";
var req = t.request('GET', '/', { host: 'www.google.com' });
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment