Instructions for the http.get method in node.js

  • 2020-05-05 10:51:45
  • OfStack

method description:

Because most requests are GET requests that do not contain the body of the request. Node.js provides an easier way to request.

This method differs from Http.request () in that it requests only in GET mode and automatically calls req.end () to end the request.

syntax:


http.get(options, callback)

Since this method belongs to the http module, the http module (var http= require(" http "))

needs to be introduced before use

receive parameters:

option           represents the domain name of the requesting website or IP address (the requested address)

The callback       callback function passes an argument res, the response object, which represents the response to be made upon receipt of the request. To see what attributes res has, see "http.response attribute integration."

res is an instance of http.ClientResponse.

Example:


http.get("http://www.google.com/index.html", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});

source code:


exports.get = function(options, cb) {
  return globalAgent.get(options, cb);
};


Related articles: