1.0.0 • Published 3 years ago

@nioh/proxy v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Controller: proxy

安装

Install with npm

npm i @server/proxy --registry=http://npm.zkh360.com

说明

代理控制器,proxy

使用

import * as Koa from 'koa'
import * as Router from 'koa-router'
import * as body from 'koa-body'
import {proxy} from '@server/proxy'

const app = new Koa()
const router = new Router()

router.get('/', ctx => {
  ctx.body = 'context'
})

router.get('/api/somefile', body(), proxy({
  host: 'somehost',
  method: 'POST',
  type: 'stream',
  filename: 'demo.xlsx'
}))

router.all('/api/*', body(), proxy({
  debug: true,
  host: 'https://api.github.com',
  rewrite: {
    '^/api': '',
  },
  // 处理 request option, option 可参考 request 官方文档
  extend: (option, ctx) => {
    // option: request option
    option.headers.Authorization = `bearer ${ctx.state.token}`
  }
}))

app.listen(3000)

option

interface Option {
  host: string
  method?: string
  rewrite?: Object
  extend?: Function
  type?: string
  filename?: string
  debug?: Boolean
}

ChangeLog

Tests

npm test

License

2019@ZKH-FE

1.0.0

3 years ago