nodejs模仿http請求組件nodegrass簡單例子

最近作數據導入,須模擬http請求,調用框架的相應方法進行數據的插入及保存操做。node

採用nodejs的nodegrass方法進行相應簡單模仿。npm

一、搭建nodejs環境。app

二、執行npm install nodegrass命令。框架

三、引入模塊,var ng= require(nodegrass);函數

四、下面先看nodegrass底層的get方法的具體實現,代碼以下:post

//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";
    }

}

從中能夠看出,只須要在方法調用中,加入以下參數便可:ui

url請求地址,this

callback回調函數,編碼

reqheaders請求頭標識,通常使用    'Content-Type': 'application/x-www-form-urlencoded'url

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

 下面以post方式模仿登陸請求爲例,代碼以下:

var ng = require('nodegrass');
var REQ_HEADERS = {
    'Content-Type': 'application/x-www-form-urlencoded'
};

ng.post("http://******/user/login",
        function (res, status, headers) {
            if (res.success) {
                console.log("登陸成功。");
            }
            else {
                console.log("登陸失敗。");
            }
        },
        REQ_HEADERS,
        {name: '*****', pwd: '***', rememberPwd: true},
        'utf8').
        on('error', function (e) {
            console.log("Got error: " + e.message);
        });

此簡單例子根據nodegrass的API爲根據書寫,若有不足之處,還請原諒。

菜鳥在前進,天天進步一點點。 

相關文章
相關標籤/搜索