zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    好的软件架构设计(转)
    利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复(转)
    Lucene.Net+盘古分词器(详细介绍)(转)
    如何让 height:100%; 起作用---父级元素必须设定高度
    前端问题汇总
    JS基础_强制类型转换-Number
    CSS实现垂直居中的常用方法
    vue项目上线步骤
    一篇文章全面了解运维监控知识体系
    vue+axios 前端实现登录拦截(路由拦截、http拦截)
  • 原文地址:https://www.cnblogs.com/wuxiang/p/4763726.html
Copyright © 2011-2022 走看看