# @umajs/plugin-proxy

> 代理转发插件

Latest version **0.0.7** (published 2022-10-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install @umajs/plugin-proxy
pnpm add @umajs/plugin-proxy
yarn add @umajs/plugin-proxy
bun add @umajs/plugin-proxy
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2022-10-14 |
| First published | 2021-06-22 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 15.1 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | BubbleM |
| Maintainers | bubblem, edwwardzzz, dazjean, cocoboom, liushaohua, wangliang826 |

## Links

- npm: https://www.npmjs.com/package/@umajs/plugin-proxy
- Repository: https://github.com/BubbleM/plugin-proxy
- Homepage: https://github.com/BubbleM/plugin-proxy#readme
- Issues: https://github.com/BubbleM/plugin-proxy/issues
- npm.io page: https://npm.io/package/@umajs/plugin-proxy

## Dependencies (5)

- [qs](https://npm.io/package/qs.md) ^6.11.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [koa2-connect](https://npm.io/package/koa2-connect.md) ^1.0.2
- [@umajs/logger](https://npm.io/package/@umajs/logger.md) ^2.0.0
- [http-proxy-middleware](https://npm.io/package/http-proxy-middleware.md) ^2.0.0

## Recent versions

- 0.0.7 (latest) — 2022-10-14
- 0.0.6 — 2022-10-08
- 0.0.4 — 2022-10-08
- 0.0.3 — 2022-09-21
- 0.0.2 — 2021-06-22
- 0.0.1 — 2021-06-22

## README

## 基于 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware) 包装的轻量级代理插件

### Install
`npm i @umajs/plugin-proxy --save`

### Basic usage
```
// config/plugin.config.ts
{
  proxy: {
    enable: true, // 开启代理插件
    options: {
      prefix: /(proxy)/,
      target: {
        baidu: 'http://www.baidu.com',
        a: 'http://www.a.com/user/getName'
      }
      timeout: 10000,
    }
  }
}
```
/proxy/${target}/xxx 会被转发到对应 /${target}/xxx
- http://localhost:8058/proxy/baidu/xxx -> http://www.baidu.com/xxx
- http://localhost:8058/proxy/a/user/getName?id=1 -> http://www.a.com/user/getName?id=1

### Options
#### prefix
> 代理网关前缀，根据访问路由进行匹配，匹配成功走转发逻辑 支持正则

prefix: new RegExp(/(proxy|api)/) 以proxy开头或api开头的会被转发

#### target
> 代理转发目标地址，可通过路径指定 key为目标地址的标识，value为具体目标地址
1. target: require(path.join(ROOT, '../config/index')),
```
// config/index.ts
module.exports = {
  baidu: 'http://www.baidu.com',
}
```
2. target: {
    baidu: 'http://www.baidu.com',
   }
#### timeout
> 网关超时时间，单位毫秒，默认 10000 毫秒

---
_Source: https://npm.io/package/@umajs/plugin-proxy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
