Nodejs Imitation HTTP request Component Nodegrass simple example

Source: Internet
Author: User

Recently do the data import, must simulate the HTTP request, calls the framework the corresponding method carries on the data inserting and saves the operation.

The Nodegrass method of Nodejs is used for the corresponding simple imitation.

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) {varprotocol =getprotocol (URL); var_defaultcharset = ' UTF8 '; if(typeofCharSet = = = ' String ') {_defaultcharset=CharSet; }    if(typeof(reqheaders) = = = "string" && charset = = =undefined) {_defaultcharset=reqheaders; }    varNewheader = {}; if(Reqheaders!== undefined &&typeof(reqheaders) = = = "Object") {         for(varEleinchreqheaders) {newheader[ele.tolowercase ()]=Reqheaders[ele]; }} newheader["Content-length"] = 0; varOptions ={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 don't 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 Header//@param postdataNodeGrass.prototype.post =function(url,callback,reqheaders,data,charset) {varprotocol =getprotocol (URL); var_defaultcharset = ' UTF8 '; if(typeofCharSet = = = ' String ') {_defaultcharset=CharSet; }    if(typeof(data) = = = ' object ') {data =querystring.stringify (data);} varoptions={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 don't 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 following is an example of a post-emulation login request, as follows:

varng = require (' Nodegrass ');varReq_headers = {    ' Content-type ': ' application/x-www-form-urlencoded '};ng.post ("Http://******/user/login",        function(res, status, headers) {if(res.success) {Console.log ("Login successful. "); }            Else{Console.log ("Login failed. "); }}, Req_headers, {name:' * * * * *, PWD: ' * * * ', rememberpwd:true},        ' UTF8 '). On (' Error ',function(e) {Console.log ("Got Error:" +e.message); });

This simple example is written according to Nodegrass's API, and if there are deficiencies, please forgive.

Rookie in advance, a little progress every day.

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.