# http-proxy-rules

> A proxy rules add-on to the node-http-proxy module.

Latest version **1.1.3** (published 2018-01-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install http-proxy-rules
pnpm add http-proxy-rules
yarn add http-proxy-rules
bun add http-proxy-rules
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2018-01-05 |
| First published | 2015-11-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 126 |
| Author | Don Mai |
| Maintainers | donasaur |
| Keywords | node-http-proxy, http-proxy, http-proxy-table, proxy-table, proxy-rules, proxy |

## Links

- npm: https://www.npmjs.com/package/http-proxy-rules
- Repository: https://github.com/donasaur/http-proxy-rules
- Homepage: https://github.com/donasaur/http-proxy-rules#readme
- Issues: https://github.com/donasaur/http-proxy-rules/issues
- npm.io page: https://npm.io/package/http-proxy-rules

## Alternatives

- [@lexical/table](https://npm.io/package/@lexical/table.md) — 3.0M weekly downloads
- [mantine-datatable](https://npm.io/package/mantine-datatable.md) — 98.2K weekly downloads
- [react-native-collapsible-tab-view](https://npm.io/package/react-native-collapsible-tab-view.md) — 70.6K weekly downloads
- [@handsontable/vue3](https://npm.io/package/@handsontable/vue3.md) — 16.1K weekly downloads
- [vuewordcloud](https://npm.io/package/vuewordcloud.md) — 7.2K weekly downloads

## Recent versions

- 1.1.3 (latest) — 2018-01-05
- 1.1.2 — 2018-01-05
- 1.1.1 — 2017-09-19
- 1.1.0 — 2017-02-10
- 1.0.1 — 2016-01-21
- 1.0.0 — 2015-11-21
- 0.0.1 — 2015-11-13

## README

http-proxy-rules
======

[![Build Status](https://travis-ci.org/donasaur/http-proxy-rules.svg?branch=master)](https://travis-ci.org/donasaur/http-proxy-rules)

`http-proxy-rules` is an add-on module to the [node-http-proxy](https://github.com/nodejitsu/node-http-proxy) library. It lets you define a set of rules to translate matching routes to target routes that the reverse proxy service will talk to on the client's behalf.

## Installation
```sh
npm install http-proxy-rules --save
```

## Example Use Case
```js
  var http = require('http'),
      httpProxy = require('http-proxy'),
      HttpProxyRules = require('http-proxy-rules');

  // Set up proxy rules instance
  var proxyRules = new HttpProxyRules({
    rules: {
      '.*/test': 'http://localhost:8080/cool', // Rule (1)
      '.*/test2/': 'http://localhost:8080/cool2/', // Rule (2)
      '/posts/([0-9]+)/comments/([0-9]+)': 'http://localhost:8080/p/$1/c/$2', // Rule (3)
      '/author/([0-9]+)/posts/([0-9]+)/': 'http://localhost:8080/a/$1/p/$2/' // Rule (4)
    },
    default: 'http://localhost:8080' // default target
  });

  // Create reverse proxy instance
  var proxy = httpProxy.createProxy();

  // Create http server that leverages reverse proxy instance
  // and proxy rules to proxy requests to different targets
  http.createServer(function(req, res) {

    // a match method is exposed on the proxy rules instance
    // to test a request to see if it matches against one of the specified rules
    var target = proxyRules.match(req);
    if (target) {
      return proxy.web(req, res, {
        target: target
      });
    }

    res.writeHead(500, { 'Content-Type': 'text/plain' });
    res.end('The request url and path did not match any of the listed rules!');
  }).listen(6010, cb);
```

Given the object we used to initialize the `HttpProxyRules` instance above, here are some [**examples**](test/index.tests.js#L33) of how sample url paths would be translated.

## Options

You can initialize a new `http-proxy-rules` instance with the following options:

```js
{
  rules: {}, // See notes below
  default: '' // (optional) if no rules matched, translate url path to specified default
}
```
The rules object contains a set of key-value pairs mapping a regex-supported url path to a target route. The module only tries to match the visited url path, and not the entire url, with a specified rule. The target route must include the protocol (e.g., http) and the FQDN. You can use capturing groups when constructing a rule key (e.g. `'/posts/(\d+)/`). In this case, `$1` in the target path will be replaced with the value from the first capturing group, `$2` with the second one, and so on. See the [tests](test/index.tests.js) for examples of how incoming route url paths may be translated with the use of this module.

## Other Notes
* `(?:\\W|$)` is appended to the end of the regex-supported url path, so that if there is a key like  `.*/test` in the rules, the module matches paths `/test`, `/test/`, `/test?` but not `/testing`.
* As long as object keys continued to be ordered in V8, if there are multiple rules that match against a given url path, the module will pick the matching rule listed first for the translation.

## License
[MIT](LICENSE)

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