The http. request Method in node. js is described in node. jsrequest.
Method description:
The function room acts as a client to initiate a request to the HTTP server.
Syntax:
Copy codeThe Code is as follows:
Http. get (options, callback)
Because this method belongs to the http module, the http module (var http = require ("http") must be introduced before use "))
Receiving parameters:
Option array object, which includes the following parameters:
Host: The domain name or IP address of the requested website ). The default value is 'localhost '.
Hostname: the server name. The host name is the preferred value.
Port: port of the requesting website. The default value is 80.
LocalAddress: The local location where the network connection is established
SocketPath: Unix Domain Socket (Domain Socket path)
Method: Specifies the HTTP request method. The default value is 'get '.
Path: the path of the Request relative to the root. The default value is '/'. QueryString should be included. For example:/index.html? Page = 12
Headers: request header object.
Auth: Basic Authentication (Basic Authentication). This value is calculated as the Authorization part in the request header.
Callback: callback. A parameter is passed to the http. ClientResponse instance. Http. request returns an http. ClientRequest instance.
Example:
Copy codeThe Code is as follows:
Var options = {
Hostname: 'www .google.com ',
Port: 80,
Path: '/upload ',
Method: 'post'
};
Var req = http. request (options, function (res ){
Console. log ('status: '+ res. statusCode );
Console. log ('headers: '+ JSON. stringify (res. HEADERS ));
Res. setEncoding ('utf8 ');
Res. on ('data', function (chunk ){
Console. log ('body: '+ chunk );
});
});
Req. on ('error', function (e ){
Console. log ('problem with request: '+ e. message );
});
// Write data to request body
Req. write ('data \ n ');
Req. write ('data \ n ');
Req. end ();