PrefaceIn the browser, if we want to make a request, we used to use xhr, but this low-level API is often called in a simple way. In order to improve development efficiency, jQuery's $.ajax may be the best choice. Fortunately, a more modern fetch api appeared later. However, considering the compatibility of fetch, and the fact that it does not support some global configurations and request interruption, in actual use, we may use the axios request library to make some requests. In Node.js, almost all requests are made through the request library. Unfortunately, request stopped maintenance two years ago, and it is not easy to find a replacement library in Node.js. In the request issues[1], there is a table that recommends some commonly used request libraries in Node.js: Axios, which is widely used in browsers, is not easy to use in Node.js, especially when uploading files, there will be many unexpected problems. Recently, when I was surfing the Internet, I found that Node.js officially has a request library: undici. The name is quite complicated. So, today's article will introduce undici. By the way, undici means 11 in Italian. It seems that Double 11 is coming soon, which is good for Moutai.
Get StartedWe can install undici directly through npm:
undici exposes an object to the outside world, which provides several APIs:
undici.fetchNote: This method requires node version >= v16.5.0 Before requesting the service through undici.fetch, you need to start a simple login service through koa.
The above code is very simple and only supports accepting a POST method to the /login route. Next, we use undici.fetch to initiate a POST request.
If the request method is changed to GET, 404 will be returned.
undici.requestThe calling method of undici.request is similar to undici.fetch, and the parameter passing format is also similar .
The only difference is that the returned result is a little different. The http response result returned by the request method is in the body attribute, and this attribute also supports methods such as .json()/.text() similar to fetch. Interrupt RequestInstall the abort-controller library, then instantiate the abort-controller and pass the interrupt signal into the request configuration.
We run the code and find that the request is successful because we did not actively call the interrupt method.
Now when you run the code, you will find that there is no log of successful request output. The catch logic is entered and the request is successfully interrupted. undici.steamThe undici.steam method can be used to download files or interface proxy. Download the file
Interface Proxy
SummarizeThis article only introduces the usage of several undici APIs. It seems that the ease of using undici is still relatively low. However, the compatibility is not very good. For example, fetch only supports [email protected] and above. For this kind of relatively new library, I personally recommend waiting for a while. Although request has been abandoned, we still use some time-tested libraries, such as urllib[7] used in the egg framework, and node-fetch[8], which is relatively easy to use and is used in the same way as the fetch API in the browser. References [1]issues: https://github.com/request/request/issues/3143 [2]node-fetch: https://www.npmjs.com/package/node-fetch [3]got: https://www.npmjs.com/package/got [4]axios: https://www.npmjs.com/package/axios [5]superagent: https://www.npmjs.com/package/superagent [6]urllib: https://www.npmjs.com/package/urllib [7]urllib: https://www.npmjs.com/package/urllib [8]node-fetch: https://www.npmjs.com/package/node-fetch |
<<: Our company’s “Double 11” flow control plan, come and copy our homework!
>>: China Mobile: 5G package customers reach 331 million
[[382428]] From the perspective of technological ...
Hardware maintenance has always been a major prob...
SiliCloud is a foreign hosting service provider e...
【51CTO.com Quick Translation】As we all know, API ...
[51CTO.com original article] On November 13, the ...
With the release of the operating data of the thr...
Gartner's report shows that by 2022, 75% of d...
Speaking of the Communications Design Institute, ...
According to Zhongguancun Online, relevant person...
According to foreign media, Vietnam's Ministr...
If you need a high-defense server or an unlimited...
Listen to the strongest voice of open source in C...
Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN ...
KVMCloud appeared on the tribe for the first time...