美文网首页
模拟用户请求的包 -- superagent [百万级]

模拟用户请求的包 -- superagent [百万级]

作者: squidbrother | 来源:发表于2020-03-10 23:33 被阅读0次
概述

小型的渐进式客户端HTTP请求库,以及具有相同API的Node.js模块,支持许多高级HTTP客户端特性
Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features

周下载(Weekly Downloads)
3,350,473

安装:npm install superagent -S

使用:

const superagent = require('superagent');
 
// callback
superagent
  .post('/api/pet')
  .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
  .set('X-API-Key', 'foobar')
  .set('accept', 'json')
  .end((err, res) => {
    // Calling the end function will send the request
  });
 
// promise with then/catch
superagent.post('/api/pet').then(console.log).catch(console.error);
 
// promise with async/await
(async () => {
  try {
    const res = await superagent.post('/api/pet');
    console.log(res);
  } catch (err) {
    console.error(err);
  }
})();

相关文章

网友评论

      本文标题:模拟用户请求的包 -- superagent [百万级]

      本文链接:https://www.haomeiwen.com/subject/sukodhtx.html