Scoped HTTP Client for Node.js
Node.js's HTTP client is great, but a little too low level for common purposes. It's common practice for some libraries to extract this out so it's a bit nicer to work with.
{ var client = http client // ...}
I hate functions with lots of optional arguments. Let's turn that into:
var scopedClient = util = var client = scopedClient { util }
You can scope a client to make requests with certain parameters without affecting the main client instance:
client // reset pathclientscope'users/technoweenie' { // cli's path is "https://api.github.com/users/technoweenie" cli { util }} // client's path is back to just "https://api.github.com"
You can use .post()
, .put()
, .del()
, and .head()
.
client scope'users/technoweenie' { var data = JSON // posting data! cli { util } }
Sometimes you want to stream the request body to the server. The request is a standard http.clientRequest.
client { // ...}
And other times, you want to stream the response from the server. Simply listen for the request's response event yourself and omit the response callback.
client
Basic HTTP authentication is supported:
client
Adding simple timeout support:
client = ScopedClient.create('http://10.255.255.1:9999');
client.timeout(100);
client.get()(function(err, resp, body) {
if (err) {
util.puts("ERROR: " + err);
}
});
Development
Run this in the main directory to compile coffeescript to javascript as you go:
$ coffee -wc -o lib --no-wrap src/**/*.coffee
Copyright
Copyright (c) 2014 rick. See LICENSE for details.