Abstract : stay npm Checked the official website request The current state of the dependent package , As expected 2020 It was abandoned in .
This article is shared from Huawei cloud community 《npm in request Dependent package has been deprecated ? Salute and investigate alternatives !》, author : gentle_zhou.
In this month's code review , Find a js Still used in the project request Rely on the package to download , Vaguely remember that this dependency package should be in 2019 It has been in maintenance since ( That is, only the current code will be maintained , Repair bug, But will not receive new features or release major versions ).
Plus what I've been doing recently MD5 File check , But every time I pass request Method to download the resources MD5 evaluation , The results are different , So it's right request Reliance on packages raises doubts ( Later, after development and testing, it was found that , Actually sum request It doesn't matter to rely on packages ,request Still usable ; But because it's deprecated , It is suggested to replace ).
So immediately npm Check the current status of the dependency package on the official website , As expected 2020 It was abandoned in . Links to the official website :https://www.npmjs.com/package/request.
why request Dependent packages are deprecated
The official website said ,2020 year 2 month 11 Japan ,npm Dependent packages are completely discarded ( That is, there will be no new changes ); It's a great pity , This 2009 The dependency package that emerged in can be said to be JavaScript A very heavyweight in the ecological world .
stay 2019 In the year , There is 41K The module depends on it to download , And every week 14,000,000 Downloads .
So much use and download , This leads developers to think that this will bring two significant bad effects to dependent packages :1. It will become very difficult for new dependent packages to develop similar requirements , because request It currently occupies the ecosystem (Respect! ∠(°^°)); 2. So much reliance and use , Lead to any meaningful change in the future ( Significant changes ) The execution of requests can be very difficult , Because this change may not be adopted by most of its dependencies , And it will make it with thousands of uses request The blog and stack overflow responses of dependent packages are inconsistent .
Plus with JS The development of , In certain circumstances ,request The core part of the dependency package can't keep up with the trend of the times . for instance , Most developers will use async/await and promises Mix and match , This pattern is in version 8 Of Node.js Used for the first time in , however request Does not support . At this time, the second bad effect that developers say limits request To develop, to change ( Yes request Making significant changes can lead to a lot of conflict ).
therefore , This is in 2022 year 2 month 14 When I went to check on the day github Upper quilt star 了 25.4K My project is in 2020 year 2 month 11 The day was abandoned .RIP.
request What is a dependency package for
Take the last one npm Official website request Screenshot of dependent package :
Let's introduce... From the picture , As you can see, this is equivalent to a very easy to use simple HTTP client ( It is also very easy to use and simple in practical use );request Dependency packages are created to help users simply deal with HTTP call , It can help users do configuration work , Save users a lot of complicated work ( Such as configuration HTTP Connect to the agent , Or make a POST request ).
The simplest practical operation is as follows ( Just reference the dependent package , then request To download , return error, response, body Information ):
const request = require('request'); request('http://www.google.com', function (error, response, body) { console.error('error:', error); // Print the error if one occurred console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received console.log('body:', body); // Print the HTML for the Google homepage. });
request Alternatives to relying on packages
So is there request What alternatives can we use ? After doing some research , Found that the following rely on the latest Node.js-8.x Of “ Popular and popular ” Dependency package :
- Got:Human-friendly and powerful HTTP request library for Node.js. Lightweight user friendly HTTP Request Library .
https://www.npmjs.com/package/got - Axios:Promise-based HTTP client for the browser and Node.js.
https://www.npmjs.com/package/axios - Node Fetch:A light-weight module that brings window.fetch to Node.js. A lightweight HTTP Request Library , It will the browser's Fetch API Function introduction Node.js.
https://www.npmjs.com/package/node-fetch - Superagent:Small progressive client-side HTTP request library, and Node.js module with the same API sporting many high-level HTTP client features. A similar to Axios The popularity of HTTP library , Used in Node.js And in the browser AJAX request .
https://www.npmjs.com/package/superagent
Performance comparison of several functions supported by packages , Refer to the figure below :
therefore , Through the above figure and our actual test , Come to the following conclusion :
- Axios There are more days / Zhou / Monthly downloads ,github Get more stars on ; added followers And more fork, Support in browser Upper use , However, the progress display only supports the display on the browser .
- Got There are more versions , More frequent updates , Less open issues And less open pull requests, But there is one obvious one issue( According to our team Xie Hanhua wx1079540 Test of ): Download the latest version of 12.0.1, There will be errors in use - Use await go Introducing packages can lead to no response, Only use got Introducing packages can lead to "Must use import to load ES Module:" The alarm
– https://github.com/sindresorhus/got/discussions/1978
And if you use the previous version 11.8.3, It will happen on the web CloudIDE Connection timeout error on ( Doubt and proxy of ):
– https://github.com/sindresorhus/got/issues/1572.
Another alternative is to use Node.js Standard library HTTP and HTTPS modular , No need to install external packages , But it also brings the disadvantage that it is not friendly to use , It's going to be a lot more complicated .
Thank you for your link sharing ~
- https://www.twilio.com/blog/2017/08/http-requests-in-node-js.html
- https://cloud.tencent.com/developer/article/1735008
- http://nodejs.cn/learn/making-http-requests-with-nodejs/# perform -get- request
Code demo:
const https = require('https'); https.get(url, (res : any) => { console.log(` Status code : ${res.statusCode}`) res.on('data', d => { process.stdout.write(d) }) res.on('end', () => { console.log(` Has ended `) }) }).on('error', error => { console.error(error) })
notes :
in the light of JS Streaming download in the project , It's not just https.get(requestUrl) After downloading , It's really finished downloading ; We need to be right about const stream = fs.createWriteStream(toolsPath); This stream Constant to detect , See if it is finish Status quo .
When https.get yes end State and stream yes finish In state , We can say that all downloads are complete ; Only then can MD5 Verification of values .
Reference link
- https://www.npmjs.com/package/request
- https://betterprogramming.pub/request-has-been-deprecated-a76415b4910b?gi=3fd5d3ad02db
- https://github.com/request/request/issues/3142
Click to follow , The first time to learn about Huawei's new cloud technology ~