Node.js modules you should know about: request
2013-07-24 11:20
441 查看
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:
Pow! You just streamed the response of HTTP request to
Here is more awesome stuff:
Pow! It streamed your local file
Pow! This just streamed a HTTP GET from
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:
Install it via npm, as always:
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!
source : http://www.catonmat.net/blog/nodejs-modules-request/
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.pnginto
doodle.pnglocal 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.jsonto
http://mysite.com/obj.jsonas 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.pngto 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!
source : http://www.catonmat.net/blog/nodejs-modules-request/
相关文章推荐
- Four Node.js Gotchas that Operations Teams Should Know about
- Data Structure: All you should know about Hash
- Data Structure: All you should know about Hash
- Data Structure: All you should know about Hash
- What You Should Know About Floats
- 6 Things You Should Know About Fragment URLs
- Data Structure: All you should know about Hash
- 6 Things You Should Know About Fragment URLs
- Data Structure: All you should know about Hash
- What You Should Know About the Jewelry Business
- 6 Things You Should Know About Fragment URLs
- 10 Things You Should Know About Microsoft's Silverlight
- 7 Python Libraries you should know about
- 15 Things You Should Know about the ORDER BY Clause
- Data Structure: All you should know about Hash
- Modules you should know in Python Libray
- 5 Things You Should Know About the New Maxwell GPU Architecture
- 5 things you should know about SharePoint 2007 Records Center
- About JAVA ,You Should Know-程序员的自我修养
- 5 XSS Exploits You Should Know About