Axios库和SuperAgent库有什么区别?

Ish*_*tel 3 javascript superagent axios

我正在学习JavaScript,并且可以看到在多个大型项目中,SuperAgent用于HTTP请求。我将Axios用于学习目的,但想知道是什么使SuperAgent与Axios不同?

dim*_*uel 9

superagent并且axios是 HTTP 客户端库。他们都非常成熟,在两者之间进行选择最终归结为偏好。下面是POST使用每个库使用 JSON 主体发出请求的样子:

// superagent
await request
  .post('/to/my/api') // URI
  .set('Authorization', authorizationKey) // Header
  .send({ foo: 'bar' })  // Body
  // then creates a promise that returns the response
  .then(res => res.body) 
Run Code Online (Sandbox Code Playgroud)
/* axios */
// axios exclusively returns promises
// URI, body, request options are provided in one call
await request.post('/to/my/api', { 
  foo: 'bar'
}, {
  headers: {
    Authorization: authorizationKey
  }
}) 
Run Code Online (Sandbox Code Playgroud)


Qiu*_*ang 6

axios有比超级特工更多的星星,请点击这里

如果您在做前端,那么axios可能会更受欢迎,例如vue使用axios,我在做后端,这两种方法都可以。

但是就像Axios vs Superagent中的一个回答说:“我将基于其他因素做出决定,例如您更喜欢哪种API,以及库大小”,我尝试了两者,最后选择了supergent b / c superagent具有内置重试功能

axios不提供重试,https://github.com/axios/axios/issues/164。我真的不喜欢引入另一个模块以进行重试的想法,更不用说现在已经有2个不同的模块,axios-retryvs retry-axios

此外,通过我的有限测试,此问题https://github.com/axios/axios/issues/553尚未完全解决。

  • 在我写这篇文章时,axios-retry 或 retry-axios 都不能与 axios 0.19.0(4 个月前发布)一起使用 1. https://github.com/softonic/axios-retry/issues/94 2. https: //github.com/JustinBeckwith/retry-axios/issues/62 (2认同)