mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-08 17:37:24 +08:00
64 lines
1.8 KiB
Markdown
64 lines
1.8 KiB
Markdown
# Koa Proxies
|
|
|
|

|
|
[](https://travis-ci.org/vagusX/koa-proxies)
|
|
[](https://circleci.com/gh/vagusX/koa-proxies)
|
|
[](https://codecov.io/gh/vagusX/koa-proxies)
|
|
[](https://www.npmjs.com/package/koa-proxies)
|
|
[](https://greenkeeper.io/)
|
|
|
|
> [Koa@2.x/next](https://github.com/koajs/koa) middlware for http proxy
|
|
|
|
Powered by [`http-proxy`](https://github.com/nodejitsu/node-http-proxy).
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
$ npm install koa-proxies --save
|
|
```
|
|
|
|
## Options
|
|
|
|
### http-proxy events
|
|
|
|
```js
|
|
options.events = {
|
|
error (err, req, res) { },
|
|
proxyReq (proxyReq, req, res) { },
|
|
proxyRes (proxyRes, req, res) { }
|
|
}
|
|
```
|
|
|
|
### log option
|
|
```js
|
|
// enable log
|
|
options.logs = true; // or false
|
|
|
|
// custom log function
|
|
options.logs = (ctx, opts.target) {
|
|
console.log('%s - %s %s proxy to -> %s', new Date().toISOString(), ctx.req.method, ctx.req.oldPath, new URL(ctx.req.url, target))
|
|
}
|
|
```
|
|
|
|
## Usage
|
|
|
|
```js
|
|
// dependencies
|
|
const Koa = require('koa')
|
|
const proxy = require('koa-proxies')
|
|
const httpsProxyAgent = require('https-proxy-agent')
|
|
|
|
const app = new Koa()
|
|
|
|
// middleware
|
|
app.use(proxy('/octocat', {
|
|
target: 'https://api.github.com/users',
|
|
changeOrigin: true,
|
|
agent: new httpsProxyAgent('http://1.2.3.4:88'), // if you need or just delete this line
|
|
rewrite: path => path.replace(/^\/octocat(\/|\/\w+)?$/, '/vagusx'),
|
|
logs: true
|
|
}))
|
|
```
|
|
|
|
[](https://github.com/feross/standard)
|