SuperAgent 是一个轻量的Ajax API,服务器端(Node.js)客户端(浏览器端)均可使用,SuperAgent具有学习曲线低、使用简单、可读性好的特点,可作为客户端请求代理模块使用,当你想处理get,post,put,delete,head请求时,可以考虑使用SuperAgent。

const superagent = require('superagent')

/**
 * 接口请求封装  使用superagent发送请求
 * @param {*} url 
 * @param {*} method 
 * @param {*} params 
 * @param {*} data 
 * @param {*} cookies 
 */
function req(url, method, params, data, cookies) {
	return new Promise(function (resolve, reject) {
		superagent(method, url)
			.query(params)
			.send(data)
			.set('Content-Type', 'application/x-www-form-urlencoded')
			.end(function (err, response) {
				if (err) {
					reject(err)
				}
				resolve(response)
			})
	})
}
module.exports = {
	req
}

  

posted on 2021-07-03 17:28  逻辑短路  阅读(74)  评论(0编辑  收藏  举报