One, the HTTP module provides two functions http.request and Http.get, the function is to initiate a request to httpserver as a client.
Ext.Ajax.request ({},function (response))
1.http.request (options,callback) initiates an HTTP request that accepts two parameters, and option is an object that resembles an associative array.
Represents the requested number of parameters, callback is the requested callback function, and the option is used frequently for example
Host: The domain name or IP address of the requesting site
Port: Ports of the requesting site, default is 80,
Method: Request methods, Mode is Get/post
Path: The relative root path of the request, default is "/". QueryString should be included, such as/search?query=marico
Headers: An associative array object that is the content of the request header
Callback passes a number of parameters for HTTP. Examples of Clientresponse
Http.request returns an instance of a http.clientrequest
Clientrequest.js
var http=require (' http ');
var querystring=require (' QueryString ');
Start the service
Http.createserver (function (req,res) {
Console.log (' request arrival, resolution of the number of references ');
var post= ';
Req.on (' Data ', function (chunk) {
Post+=chunk;
});
Req.on (' End ', function () {
Post=querystring.parse (POST);
Parsing complete
Console.log (' parameter parsing is complete, return name parameter ');
Res.end (Post.name);
});
}). Listen (3000, ' 127.0.0.1 ');
Client request
var contents=querystring.stringify ({
Nane: ' Octopus ',
AGE:20,
Address: ' Beijing '
});
var options={
Host: ' localhost ',
Path: '/',
port:3000,
Method: ' POST ',
headers:{
' Content-type ': ' application/x-www-form-urlencoded ',
' Content-length ': contents.length
}
};
var req=http.request (Options,function (res) {
Res.setencoding (' Utf-8 ');
Res.on (' Data ', function (data) {
Console.log (' Backend return data ');
Console.log (data);
})
});
Req.write (contents);
Req.end ();
The 2.http.get (options,callback) HTTP module also provides a more convenient way to handle get requests: Http.get. It's a simplified version of Http.request,
The only difference is that Http.get itself takes the initiative to set the request method to get request, at the same time does not need to manually call Req.end ();
Example: Clientget.js
var http=require (' http ');
var url=require (' url ');
var util=require (' Util ');
Start the service
Http.createserver (function (req,res) {
Console.log (' request arrival, resolution of the number of references ');
var params=url.parse (req.url,true);
Console.log (' parse complete ');
Console.log (Util.inspect (params));
Console.log (' Return to client ');
Res.end (Params.query.name);
}). Listen (3000);
http.get ({
' host ': ' localhost ',
path: '/user?name=octopus&age=20 ',
port:3000},
function (res) {
res.setencoding (' utf-8 ');
res.on (' Data ', function (data) {
Console.log (' service-side response back data: ' +data ');
})
});
two, http. Clientrequest
This object is an object produced by Http.request or Http.get, representing an HTTP request that has been generated and in progress, which provides the response event,
That is, HTTP. Request or http.get the binding object of the callback function formulated by the second parameter, the requests must call the end method to close the request.
Functions provided:
Request.abort () terminating the request being sent
request.settimeout (Timeout,[callback]) Set the request time-out time, timeout is the number of milliseconds, and when the request times out, callback will be called
Other: Request.setnodelay ([Nodelay]), Functions such as request.setscoketkeepalive ([Enable],[initialdelay]).
api Address: http://nodejs.org/api/http.html
Third, http. Clientresponse
http. Clientreponse is similar to Http.serverresponse, which provides three events, data, end, and close, which are triggered at the end of the arrival, transfer, and connection, respectively.
The data event passes a parameter chunk that represents the received
property that represents the result status of the request
StatusCode HTTP status code, such as 200,404,500
Httpversion:http Protocol version number
Headers:http Request Header
Trailers:http Request Tail
Function:
Response.setencoding ([encoding]): Sets the default encoding, which is encoded with encoding when the data event is triggered. The default value is NULL, which is stored as buffer.
Response.pause (): Pause to accept data and send events for easy download.
Response.resume (): Recovering in a paused state