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