nodejs模仿http请求组件nodegrass简单例子
1、搭建nodejs环境。
2、执行npm install nodegrass命令。
3、引入模块,var ng= require(nodegrass);
4、下面先看nodegrass底层的get方法的具体实现,代码如下:
//Get Method Request
//Support HTTP and HTTPS request,and Automatic recognition
//@Param url
//@Param callback
NodeGrass.prototype.get = function(url,callback, reqheaders, charset){
var protocol = getProtocol(url);
var _defaultCharSet = 'utf8'; if(typeof charset === 'string' ){
_defaultCharSet = charset;
}
if(typeof(reqheaders) === "string" && charset === undefined) {
_defaultCharSet = reqheaders;
}
var newheader = {};
if(reqheaders !== undefined && typeof(reqheaders) === "object") {
for(var ele in reqheaders) {
newheader[ele.toLowerCase()] = reqheaders[ele];
}
}
newheader["content-length"] = 0;
var options = {
host:getHost(url),
port:getPort(url),
path:getPath(url),
method:'GET',
headers:newheader
}; if(protocol === http || protocol === https){
return _sendReq(protocol,null,options,_defaultCharSet,callback);
}else{
throw "sorry,this protocol do not support now";
} }
从中可以看出,只需要在方法调用中,加入如下参数即可:
url请求地址,
callback回调函数,
reqheaders请求头标识,一般使用 'Content-Type': 'application/x-www-form-urlencoded'
charset编码方式,一般为utf8
对应的post请求的底层实现代码如下:
//Post Method Request
//Support HTTP and HTTPS request,and Automatic recognition
//@Param url
//@Param callback
//@Param header
//@param postdata
NodeGrass.prototype.post = function(url,callback,reqheaders,data,charset){
var protocol = getProtocol(url);
var _defaultCharSet = 'utf8'; if(typeof charset === 'string' ){
_defaultCharSet = charset;
} if(typeof(data) === 'object'){data = querystring.stringify(data);}
var options={
host:getHost(url),
port:getPort(url),
path:getPath(url),
method:'POST',
headers:reqheaders
};
if(protocol === http || protocol === https){
return _sendReq(protocol,data,options,_defaultCharSet,callback)
}else{
throw "sorry,this protocol do not support now";
}
}
从中可以看出,只需要在方法调用中,加入如下参数即可:
url请求地址,
callback回调函数,
reqheaders请求头标识,一般使用 'Content-Type': 'application/x-www-form-urlencoded'
data请求所包含的具体数据,
charset编码方式,一般为utf8
封装代码如下:
var nodegrass = require('nodegrass');
var REQ_HEADERS = {
'Content-Type' : 'application/x-www-form-urlencoded'
};
// get请求
exports.ngGet = function(url, params, encoding) {
if (!encoding) {
encoding = "UTF-8";
}
var result = "";
nodegrass.get(url, function(data, status, headers) {
console.log(status);
console.log(headers);
console.log(data);
result = data;
}, REQ_HEADERS, params, encoding).on('error', function(e) {
console.log("Got error: " + e.message);
});
return result;
};
// post请求
exports.ngPost = function(url, params, encoding) {
if (!encoding) {
encoding = "UTF-8";
}
var result = "";
nodegrass.post(url, function(data, status, headers) {
console.log(status);
console.log(headers);
console.log(data);
result = data;
}, REQ_HEADERS, params, encoding).on('error', function(e) {
console.log("Got error: " + e.message);
});
return result;
}
nodejs模仿http请求组件nodegrass简单例子的更多相关文章
- jquery ajax跨域请求后台的简单例子
一.简介AJAX 是与服务器交换数据的技术,它在不重载全部页面的情况下,实现了对部分网页的更新. ajax() 方法通过 HTTP 请求加载远程数据. 该方法是 jQuery 底层 AJAX 实现.简 ...
- jquery ajax请求后台 的简单例子
jQuery.ajax(url,[settings]) 概述 通过 HTTP 请求加载远程数据. jQuery 底层 AJAX 实现.简单易用的高层实现见 $.get, $.post 等.$.ajax ...
- NodeJS+Express+Socket.io的一个简单例子
关键字:NodeJS,Express,Socket.io. OS:Windows 8.1 with update pro. 1.安装NodeJS:http://nodejs.org/. 2.初始化一个 ...
- vue 父组件调用子组件方法简单例子(笔记)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用nodegrass简单封装http请求例子
1.项目中经常性的使用http发送请求处理数据.而大部分请求方式为get和post,于是对http请求进行封装,提供代码的利用率. 2.nodegress是nodejs的一个请求工具. 具体步骤及代码 ...
- CANoe 入门 Step by step系列(三)简单例子的剖析【转】
最好的学习方式是什么?模仿.有人会问,那不是山寨么?但是我认为,那是模仿的初级阶段,当把别人最好的设计已经融化到自己的血液里,变成自己的东西,而灵活运用的时候,才是真正高级阶段.正所谓画虎画皮难画骨. ...
- 【转载】CANoe 入门 Step by step系列(三)简单例子的剖析
来源:http://www.cnblogs.com/dongdonghuihui/archive/2012/09/26/2704623.html 最好的学习方式是什么?模仿.有人会问,那不是山寨么?但 ...
- Spring框架系列(2) - Spring简单例子引入Spring要点
上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
随机推荐
- linux SecureCRT ssh key认证登陆
转自:http://blog.chinaunix.net/uid-20639775-id-3207171.html 通过SecureCRT创建key登录认证 一.生成公钥/密钥对 使用SecureCR ...
- Activity之间传递数据的方式及常见问题总结
Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...
- android应用程序如何调用支付宝接口
最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...
- JSON转javabean(pojo)利器
别再对着json来手写javabean啦.这个工作完全不要脑子,而且耗时. 这里给大家提供三种方式: android studio版: 万能的插件:GsonFormat 如何安装? Preferenc ...
- 纯css实现两列等高
<!doctype html> <html> <head> <meta /> <title>Title</title> < ...
- 运用datalist标签实现用户的搜索列表
datalist是一个很强大的HTML5标签,支持一般类似于模糊查询,以前都是需要js来做的.下面是一个datalist配合js的小例子,主要是实现用户是否存在,以及添加过程中是否重复的判断. 首先是 ...
- 指针与const
指向常量的指针,不能用于改变其所指对象的值. 指针的类型必须与所指对象的类型一致,但是有两个例外,第一种是允许令一个指向常量的指针指向一个非常量对象: double dra1 = 3.14; cons ...
- Collection与Map
20145217 <Java程序设计>第5周学习总结(2) 教材学习内容总结 程序中常有收集对象的需求 9.1collection架构 收集对象的行为,像是新增对象的add()方法.移除对 ...
- Codeforces Round #348(VK Cup 2016 - Round 2)
A - Little Artem and Presents (div2) 1 2 1 2这样加就可以了 #include <bits/stdc++.h> typedef long long ...
- Python基础1-Python环境搭建
Python环境搭建首先通过终端窗口输入 "python" 命令来查看本地是否已经安装Python以及Python的安装版本: 若未安装则需要下载安装,下面为linux和windo ...