y-fetch-api

1.0.5 • Public • Published

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

  1. url: interface路径,例:'/test'
  2. data: 传递参数,例:{ name: '张三' }
  3. config: 配置,包含headers,例:{ headers: { "Content-Type": "application/json" } }

备注

  1. 目前只支持json格式与form表单
  2. 文件上传暂不支持(待开发)

Package Sidebar

Install

npm i y-fetch-api

Weekly Downloads

6

Version

1.0.5

License

ISC

Unpacked Size

4.71 kB

Total Files

6

Last publish

Collaborators

  • thinking_yxf