Home  >  Article  >  Web Front-end  >  Instructions for using the http.get method in node.js_node.js

Instructions for using the http.get method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:27:112383browse

Method description:

Since most requests are GET requests that do not contain a request body. Node.js provides an easier way to make requests.

The difference between this method and Http.request() is that this method only requests in GET mode, and will automatically call req.end() to end the request.

Grammar:

Copy code The code 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 represents the domain name or IP address of the requested website (requested address)

callback The callback function passes a parameter res, which is the response object, indicating the response to be made after receiving the request. If you want to know what attributes res has, you can check "http.response attribute integration".

res is an instance of http.ClientResponse.

Example:

Copy code The code is as follows:

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:

Copy code The code is as follows:

exports.get = function(options, cb) {
Return globalAgent.get(options, cb);
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn