# koa-proxies

> Http Proxy Middleware for Koa@2.x/next

Latest version **0.12.4** (published 2023-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-proxies
pnpm add koa-proxies
yarn add koa-proxies
bun add koa-proxies
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.12.4 |
| Published | 2023-11-04 |
| First published | 2016-12-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 13.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 163 |
| Author | vagusX |
| Maintainers | vagusx |
| Keywords | koa, middleware, proxy, proxies |

## Links

- npm: https://www.npmjs.com/package/koa-proxies
- Repository: https://github.com/vagusX/koa-proxies
- Homepage: https://github.com/vagusX/koa-proxies#readme
- Issues: https://github.com/vagusX/koa-proxies/issues
- npm.io page: https://npm.io/package/koa-proxies

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [http-proxy](https://npm.io/package/http-proxy.md) ^1.18.1
- [path-match](https://npm.io/package/path-match.md) ^1.2.4

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.12.4 (latest) — 2023-11-04
- 0.12.3 — 2022-12-05
- 0.12.2 — 2022-02-23
- 0.12.1 — 2021-04-08
- 0.11.0 — 2020-02-09
- 0.10.0 — 2020-02-07
- 0.9.0 — 2019-10-11
- 0.8.1 — 2018-10-27
- 0.8.0 — 2018-10-26
- 0.7.0 — 2018-04-04
- 0.6.2 — 2017-12-10
- 0.6.1 — 2017-08-29
- 0.6.0 — 2017-08-04
- 0.5.2 — 2017-07-30
- 0.5.1 — 2017-07-27
- … 5 more at https://npm.io/package/koa-proxies/versions

## README

# Koa Proxies

![NPM](https://img.shields.io/npm/v/koa-proxies.svg)

[![Node.js CI](https://github.com/vagusX/koa-proxies/actions/workflows/node.js.yml/badge.svg)](https://github.com/vagusX/koa-proxies/actions/workflows/node.js.yml)
[![Coverage](https://img.shields.io/codecov/c/github/vagusX/koa-proxies.svg)](https://codecov.io/gh/vagusX/koa-proxies)
[![NPM Downloads](https://img.shields.io/npm/dm/koa-proxies.svg)](https://www.npmjs.com/package/koa-proxies)
[![Greenkeeper badge](https://badges.greenkeeper.io/vagusX/koa-proxies.svg)](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, 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
}))
```
The 2nd parameter `options` can be a function. It will be called with the path matching result (see [path-match](https://www.npmjs.com/package/path-match) for details) and Koa `ctx` object. You can leverage this feature to dynamically set proxy. Here is an example:

```js
// dependencies
const Koa = require('koa')
const proxy = require('koa-proxies')

const app = new Koa()

// middleware
app.use(proxy('/octocat/:name', (params, ctx) => {
  return {
    target: 'https://api.github.com/',
    changeOrigin: true,
    rewrite: () => `/users/${params.name}`,
    logs: true
  }})
)
```
Moreover, if the `options` function return `false`, then the proxy will be bypassed. This allows the middleware to bail out even if path matching succeeds, which could be helpful if you need complex logic to determine whether to proxy or not.


### Attention

Please make sure that `koa-proxies` is in front of `koa-bodyparser` to avoid this [issue 55](https://github.com/vagusX/koa-proxies/issues/55)

```js
const Koa = require('koa')
const app = new Koa()
const proxy = require('koa-proxies')
const bodyParser = require('koa-bodyparser')

app.use(proxy('/user', {
  target: 'http://example.com',
  changeOrigin: true
}))

app.use(bodyParser())
```

[![JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

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