Created
February 10, 2011 09:51
-
-
Save schamane/820216 to your computer and use it in GitHub Desktop.
Overwrite http client for nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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