Node.js modules you should know about: request
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!
source : http://www.catonmat.net/blog/nodejs-modules-request/
Node.js modules you should know about: request的更多相关文章
- Node.js 学习资源
这篇文章编译整理自Stack Overflow的一个如何开始学习Node.js的Wiki帖,这份资源列表在SO上面浏览接近60万次,数千个收藏和顶.特意整理发布到这里,其中添加了部分中文参考资料. 学 ...
- 基于Debian系统配置Nginx环境的Node.js应用教程
Node.js,是当前比较流行的能够动态的快速响应内容的JavaScript框架,在有些环境下比我们使用的PHP应用都能够提高效率.目 前,Node.js可以与我们常用的Nginx.Apache等服务 ...
- [转]Node.js tutorial in Visual Studio Code
本文转自:https://code.visualstudio.com/docs/nodejs/nodejs-tutorial Node.js tutorial in Visual Studio Cod ...
- [译]Node.js - Event Loop
介绍 在读这篇博客之前,我强列建议先阅读我的前两篇文章: Getting Started With Node.js Node.js - Modules 在这篇文章中,我们将学习 Node.js 中的事 ...
- 【特别推荐】Node.js 入门教程和学习资源汇总
这篇文章与大家分享一批很有用的 Node.js 入门教程和学习资源.Node 是一个服务器端的 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用 ...
- How to install Node.js on Linux
How to install Node.js on Linux Posted on November 13, 2015 by Dan Nanni Leave a comment Question: H ...
- Node.js 入门教程和学习资源汇总
这篇文章与大家分享一批很有用的 Node.js 入门教程和学习资源.Node 是一个服务器端的 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用 ...
- nw.js桌面程序自动更新(node.js表白记)
Hello Google Node.js 一个基于Google V8 的JavaScript引擎. 一个伟大的端至端语言,或许我对你的热爱源自于web这门极富情感的技术吧! 注: 光阴似水,人生若梦, ...
- node.js 89行爬虫爬取智联招聘信息
写在前面的话, .......写个P,直接上效果图.附上源码地址 github/lonhon ok,正文开始,先列出用到的和require的东西: node.js,这个是必须的 request,然发 ...
随机推荐
- Effective STL 笔记 -- Item 6 ~ 7: Container and Object Pointer
Effective STL 笔记 – Item 6 ~ 7: Container and Object Pointer 中间两次笔记被删掉了,简单补一下: Item 3 中提到如果将对象直接放入容器中 ...
- Chrome+XX_Net的上网渠道
本页面将详细演示如何在一台全新的Windows7电脑上架设起Chrome+XX_Net的上网渠道. 本文包含以下部分: 下载和安装Chrome浏览器 获取和运行XX-Net 设置代理 手动导入证书(备 ...
- Java读取本地文件(输入流)
package cn.buaa; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; imp ...
- Java输出文件到本地(输出流)
package cn.buaa; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; im ...
- Linux下的堆off-by-one的利用
这篇稿子已经投到了360安全播报,http://bobao.360.cn/learning/detail/3113.html
- Windbg在应用层调试漏洞时的应用
主要记录一些在应用层调试漏洞的技巧,不会写一些基本的命令,只记录比较有用的平时难以想到的调试方法. 1.!address eax 查看对应内存页的属性,如果poc触发异常之后就可以用这个指令看一下触发 ...
- **后台怎么处理JSON数据中含有双引号?
http://bbs.csdn.net/topics/390578406?page=1 注意是后台,不是用js另外我这个json是直接取得别人的传过来的字符串,不是我自己拼写的,所以我自己不能做到转义 ...
- USACO 5.1 Starry Night
Starry NightIOI 98 High up in the night sky, the shining stars appear in clusters of various shapes. ...
- jupyter notebook 小技巧
Converting notebooks to other formats¶ !pip install https://github.com/ipython-contrib/jupyter_contr ...
- Java 集合之 Collection
集合就是一组数的集合,就像是一个容器,但是我们应该清楚的是集合中存放的都是对象的引用,而不是真正的实体.而我们常说的集合中的对象其实指的就是对象的引用. 我们可以把集合理解为一个小型数据库,用于存放数 ...