Nodejs Imitation HTTP request Component Nodegrass simple example

Source: Internet
Author: User

1, build Nodejs environment.

2. Execute npm install nodegrass command.

3, the introduction of modules, Var ng= require (Nodegrass);

4, the following first look at the Nodegrass bottom of the Get method of the concrete implementation, the code is as follows:

//get Method request//support HTTP and HTTPS Request,and Automatic recognition//@Param url//@Param callbackNodeGrass.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[e    Le.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"; }}

  

As you can see, you just need to add the following parameters to the method call:

URL request Address,

Callback callback function,

Reqheaders Request Header identification, general use ' content-type ': ' application/x-www-form-urlencoded '

CharSet encoding method, generally UTF8

The underlying implementation code for the corresponding post request is as follows:

Post Method request//support HTTP and HTTPS request,and Automatic recognition//@Param url//@Param callback//@Param Head er//@param postdataNodeGrass.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";  }}

  

As you can see, you just need to add the following parameters to the method call:

URL request Address,

Callback callback function,

Reqheaders Request Header identification, general use ' content-type ': ' application/x-www-form-urlencoded '

The data request contains specific

CharSet encoding method, generally UTF8

The encapsulation code is as follows:

var nodegrass = require (' Nodegrass '); var req_headers = {' Content-type ': ' application/x-www-form-urlencoded '};// Get Request 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 Request 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 Imitation HTTP request Component Nodegrass simple example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.