Method description:
The function room of the function initiates a request to the HTTP server as a client.
grammar:
The code copy is as follows:
http.get(options, callback)
Since this method belongs to the http module, the http module needs to be introduced before use (var http= require("http") )
Receive parameters:
option array object, containing the following parameters:
host: Indicates the domain name or IP address of the requested website (the requested address). Default is 'localhost'.
hostname: Server name, hostname is the preferred value.
port: The port of the requested website, the default is 80.
localAddress: local network connection
socketPath: Unix Domain Socket (Domain socket path)
method: HTTP request method, default is 'GET'.
path: The path to the root of the request is '/' by default. QueryString should be included in it. For example: /index.html?page=12
headers: Request header object.
auth: Basic authentication (basic authentication), this value will be calculated as the Authorization part of the request header.
callback : Callback, passing a parameter, as an instance of http.ClientResponse. http.request Returns an instance of http.ClientRequest.
example:
The code copy 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();