This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->
node logo

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!

This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->

Comments

December 06, 2011, 18:09

Thanks. This was really helpful. Keep it coming. :-)

December 06, 2011, 21:42

Every day for the next month or two!

Wael Permalink
December 06, 2011, 19:42

Is requre a keyword in Node js?

December 06, 2011, 21:42

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')
meow
nisc Permalink
December 06, 2011, 20:09

More node posts! Yay! Everyone has had enough time to learn sed, AWK and Perl by now.

December 06, 2011, 21:43

Yeah! I am done with sed, awk and perl. Mastered it all, so should have all my readers. :)

December 06, 2011, 22:27

I think the previous comment refers to the typo in require, and namely requre

December 06, 2011, 23:04

Oops, you're right. I just noticed that. Fixed now!

voldyman Permalink
December 07, 2011, 15:05

We have, my master.... :)

nisc Permalink
December 06, 2011, 20:12

I'm not saying that I dislike your other posts, it's just that this stuff is more interesting ;-)

December 06, 2011, 21:43

I have blogged it all about sed, awk and perl. Nothing left. :)

Simon Permalink
December 06, 2011, 20:29

@Wael - yes, it's a built-in function for importing a module.

Antony Permalink
December 06, 2011, 20:53

Your "pow!" really encourages people to do random voice over, and not focus much on your NodeJS article. Good article though. Thanks for sharing.

Nek Permalink
December 07, 2011, 04:44

Thanks a lot! Very useful.
Looks much simpler than "http"/"https".

January 10, 2012, 13:37

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.

July 25, 2014, 16:03

Nice post. i m enjoying here..

Considering all the rumors and leaked information, Samsung Galaxy Note 4 might be superior over its predecessor. I’m going to list some features of Note 4 that are subtly confirmed by Samsung. Samsung states that Galaxy Note 4 would be first to have fingerprint sensor

July 25, 2014, 17:08

Thanks for the article.
Happy Friendship Day 2014.

July 25, 2014, 20:12

wow.. this website is great and so useful..

best of video call apps

wow.. this website is great and so useful..

July 25, 2014, 20:12

wow.. this website is great and so useful..

best of video call apps

wow.. this website is great and so useful..

August 03, 2014, 03:04

http://www.bloggerwing.com/2014/07/how-to-get-massive-traffic-to-your-blog.html

August 03, 2014, 03:05

http://www.bloggerwing.com/2014/07/how-to-get-massive-traffic-to-your-blog.html

September 13, 2014, 02:32

My hot pics are here http://shockedyou.Com

November 18, 2014, 04:21

How to setisfy a girl

September 13, 2014, 02:33

I am also hot

November 11, 2014, 20:32

Awesome tutorial,
I have implemented the same in my website have a look
https://www.thepairatebay.net

November 11, 2014, 20:34

https://www.thepairatebay.net/browse
https://www.thepairatebay.net/recent
https://www/thepairatebay.net/tv
https://www.thepairatebay.net/music
https://www.thepairatebay.net/top

November 27, 2014, 12:19

thanks for sharing. Merry christmas songs | New year's.

Chandru Keerthi Permalink
November 29, 2014, 18:04

It is very basic node module that needs to be install on node. this will send the request to http. Nice post informative. Thanks for sharing with us and find the list here of SBI Branches of Warangal with IFSC Codes and MICR Codes.

December 05, 2014, 18:49

Really one of the Awesome article i have read it. Have a look on my another blog of Feliz Navidad 2014 .Will be visiting your blog next time for other awesome stuff.
Well you really did and awesome Job Man. Made a Really resourceful article, contains all the new stuff and good for readers.
Also i have made a Blog on i.e. Merry Christmas 2014. Have a Look on it and waiting for the Feedback from you. Thanks for it.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "cdrom_286": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements