【问题标题】:What is the difference between Axios and SuperAgent libraries? [closed]Axios 和 SuperAgent 库有什么区别? [关闭]
【发布时间】:2019-01-19 17:56:16
【问题描述】:

我正在学习 JavaScript,我可以看到,在多个大型项目中,SuperAgent 被用于 HTTP 请求。我正在使用 Axios 进行学习,但想知道 SuperAgent 与 Axios 有何不同?

【问题讨论】:

  • 谢谢@brad,但只是在特定的上下文中回答,似乎没有涵盖所有方面。
  • 那里有一个关于 Promise API 与回调的答案。您的问题很笼统……您有什么具体问题吗?
  • 谢谢。 @Brad 我只是想简要了解这两个库,以便知道我应该使用哪个库。
  • 对两者都进行实验,使用最适合您使用的方法。两者都使用相同的底层 Node.js 库。

标签: javascript axios superagent


【解决方案1】:

axios 的星星比 superagent 还多,check here

如果你在做前端,axios 可能更受欢迎,例如vue 使用axios,我在做后端,任何一个都可以。

但就像Axios vs Superagent 中的一个答案所说“我会根据其他因素做出决定,例如您更喜欢哪个 API 以及库大小”我尝试了两者并最终选择了 supergent b /c superagent 有build-in retry

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

另外,通过我的有限测试,这个问题https://github.com/axios/axios/issues/553 还没有完全修复。

【讨论】:

【解决方案2】:

superagentaxios 是 HTTP 客户端库。他们都非常成熟,两者之间的选择最终归结为偏好。下面是使用每个库发出带有 JSON 正文的 POST 请求的样子:

// 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) 

/* 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
  }
}) 

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2011-09-20
    • 2013-03-25
    • 2023-03-08
    • 2016-07-17
    • 2015-07-17
    相关资源
    最近更新 更多