2

The latest Node.js v17.5 introduces support for the fetch API, which is very familiar to front-end students.

fetch() is a popular cross-platform HTTP client API that runs in browsers and Web/Service Workers.

Although it is currently experimentally supported in v17.5.0, if it is officially supported in future LTS versions, there is no need to rely on third-party HTTP request modules.

The fetch API provides a WHATWG standard interface to obtain resources. This is a Promise-based HTTP client that can be used to simplify HTTP requests. It is supported in the browser environment. For the compatibility implementation of the browser environment, please refer to Web/API/fetch. Consistent with using the Fetch API in the browser.

The fetch API mainly includes the following four interfaces:

  • fetch(): This method is the most commonly used for sending requests.
  • Headers: It is equivalent to the header information of response/request, which allows you to query these header information, or perform different operations for different results.
  • Request: Equivalent to a resource request.
  • Response: Equivalent to the response of the request.

Why should be used?

There are two main reasons to use fetch() in Node.js:

  • There's been a lively discussion in the Node.js community about how to evolve Node's HTTP stack in a way that is familiar to client-side developers, while working with the server programming model, how to go beyond the limitations of the current HTTP model as a core part, and how Supporting HTTP/2-3 without unduly burdening the user, and fetch() is the first step in that conversation.
  • The implementation of fetch() is based on Undici, which is located under the Node.js Github project organization, and several of its contributors are also contributors to the Node.js project, dedicated to developing fast, reliable and specification-compliant HTTP clients for Node.js , which is faster than the built-in HTTP module, because it abandons the original HTTP module and builds directly on the socket.

How is used?

Currently, fetch() in Node.js 17.5 is an experimental feature. When running a script, you need to add --experimental-fetch to use fetch() in the script.


六一
556 声望347 粉丝

SegmentFault 新媒体运营


引用和评论

0 条评论