Home >Web Front-end >JS Tutorial >Summary introduction related to nodejs http request
This article brings you a summary and introduction about nodejs http requests. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Through the http module provided by node, you can initiate http requests through the get() and request() methods it provides. get() is an encapsulation of the request() method to facilitate the initiation of get requests. If To implement post requests, the request() method needs to be encapsulated.
request() method
let req = http.request("http://www.baidu.com", (res) =>{ console.log(res.statusCode); }); req.end();//必须调用end()方法结束请求
It should be noted that when using the request() method to initiate an http request, the request() method will return a request object, and the returned request object must be called. The end() method indicates that the request action has been completed, otherwise the request will be suspended, waiting for the completion of the request action.
request() method parameter problem:
The first parameter of request() can be directly a URL address, and the second parameter is a callback function after a request is completed and the server response is received. The callback function The first parameter of request() can be a request data configuration object. The second parameter is a callback function after a request is completed and the server response is received. The callback function has only one parameter, which is the response object reslet req = http.request("http://www.baidu.com", (res) =>{ console.log(res.statusCode); }); req.end();//必须调用end()方法结束请求.
It should be noted that the request() method cannot pass the url address and the request data configuration object at the same time. It can only choose one of the two. That is to say, the request() method can only pass two parameters
get () method
let req = http.request({hostname:"www.baiduc.com",port:80}, (res) =>{ console.log(res.statusCode); }); req.end();//必须调用end()方法结束请求
When the get() method initiates an http request, the get() method will also return a request object, but there is no need to call the end() method through this request object. This is the same as request() The methods are different.
get() method parameter problem:
The parameters of the get() method and the request() method are the same, that is, there is no need to call the end() method.
Method to set request timeout
To set the request timeout, you can use the setTimeout() method of the request object returned by the get() or request() method.let req = http.get("http://www.baidu.com", (res) =>{ console.log(res.statusCode); });
If no response is received after 1 millisecond, the setTimeout callback function will be triggered. At this time, the request can be terminated directly.
2. You can also set up a timer to implement it yourself, such as:
let req = http.get("http://www.baidu.com", (res) =>{ console.log(res.statusCode); }); req.setTimeout(1,()=>{ console.log(`请求超时了`); req.abort();//终止请求 });
Get the response data
get() and request() method callback functions The response object res does not carry response data. If you want to obtain the response data, you need to listen to the data and end events through the response object.
let requestTimer = null; let req = http.get("http://www.baidu.com", (res) =>{ console.log(res.statusCode); if(res.statusCode === 200){ clearTimeout(requestTimer);//请求超时之前完成,清除超时定时器 } }); requestTimer = setTimeout(() => { req.emit("timeout", {"msg": "请求超时了"});//如果请求超时了则发送timeout事件 }, 1000); req.on("timeout",(msg) => {//监听timeout事件并终止请求 console.log(`收到timeout事件!`); req.abort(); console.log(`终止请求`); });
[Related recommendations: node.js video tutorial]
The above is the detailed content of Summary introduction related to nodejs http request. For more information, please follow other related articles on the PHP Chinese website!