Follow me on Twitter for my latest adventures!

Hey everyone! This is the fourth post in my new node.js modules you should know about article series.
The first post was about dnode - the freestyle rpc library for node, the second was about optimist - the lightweight options parser for node, the third was about lazy - lazy lists for node.
This time I'll introduce you to a very awesome module called request by Mikeal Rogers. Request is the swiss army knife of HTTP streaming.
Check this out:
var fs = require('fs')
var request = require('request');
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
Pow! You just streamed the response of HTTP request to http://google.com/doodle.png into doodle.png local file!
Here is more awesome stuff:
var fs = require('fs')
var request = require('request');
fs.readStream('file.json').pipe(request.put('http://mysite.com/obj.json'))
Pow! It streamed your local file file.json to http://mysite.com/obj.json as HTTP PUT request!
var request = require('request');
request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'))
Pow! This just streamed a HTTP GET from http://google.com/img.png to HTTP PUT to http://mysite.com/img.png.
At Browserling we use this module for streaming data to and from couchdb. Here is an example that saves a JSON document at mikeal's test couchdb:
var request = require('request')
var rand = Math.floor(Math.random()*100000000).toString()
request({
method: 'PUT',
uri: 'http://mikeal.iriscouch.com/testjs/' + rand,
multipart: [
{
'content-type': 'application/json',
'body': JSON.stringify({
foo: 'bar',
_attachments: {
'message.txt': {
follows: true,
length: 18,
'content_type': 'text/plain'
}
}
})
},
{ body: 'I am an attachment' }
]
}, function (error, response, body) {
if(response.statusCode == 201){
console.log('document saved as: http://mikeal.iriscouch.com/testjs/'+ rand);
} else {
console.log('error: '+ response.statusCode);
console.log(body);
}
})
Install it via npm, as always:
npm install request
Sponsor this blog series!
Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we'll set it up!
See ya!
If you love these articles, subscribe to my blog for more, follow me on Twitter to find about my adventures, and watch me produce code on GitHub!


Facebook
Plurk
more
GitHub
LinkedIn
FriendFeed
Google Plus
Amazon wish list
Comments
Thanks. This was really helpful. Keep it coming. :-)
Every day for the next month or two!
Is requre a keyword in Node js?
It's just a function, it's not a keyword. You can redefine it.
pkrumins$ node > typeof require 'function' > require = function (x) { console.log(x) } [Function] > require('meow') meowMore node posts! Yay! Everyone has had enough time to learn sed, AWK and Perl by now.
Yeah! I am done with sed, awk and perl. Mastered it all, so should have all my readers. :)
I think the previous comment refers to the typo in require, and namely requre
Oops, you're right. I just noticed that. Fixed now!
We have, my master.... :)
I'm not saying that I dislike your other posts, it's just that this stuff is more interesting ;-)
I have blogged it all about sed, awk and perl. Nothing left. :)
@Wael - yes, it's a built-in function for importing a module.
Your "pow!" really encourages people to do random voice over, and not focus much on your NodeJS article. Good article though. Thanks for sharing.
Pow!
Thanks a lot! Very useful.
Looks much simpler than "http"/"https".
Pow! Good post Peteris. The 'fs' method 'readStream' throws this error - reObject #
<Object>has no method 'readStream'. When I took a look at the node.js documentation I noticed that that the method starts with capital letter 'R' - 'ReadStream'. Thank you for this awesome series.
Leave a new comment