y-fetch-api
实现原理
nodejs http/https模块实现post与get请求
安装
npm install y-fetch-api
调用例子
let yFetchAPI = require('y-fetch-api')
let http = new yFetchAPI()
http.baseUrl = 'http://www.example.com:8080' //请求地址
let data = {name: 'hello word'} //请求参数
//post请求-json格式 (ps: form格式使用$post方法)
http.$postjson('/testPost', data).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
//get请求
http.$get('/testGet', data).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
参数说明 (url[,data][,config])
- url: interface路径,例:'/test'
- data: 传递参数,例:{ name: '张三' }
- config: 配置,包含headers,例:{ headers: { "Content-Type": "application/json" } }
备注
- 目前只支持json格式与form表单
- 文件上传暂不支持(待开发)