Skip to content

Commit 24e9193

Browse files
committed
Add README
1 parent 5a072e9 commit 24e9193

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
- "0.11"
5+
- "4"

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
http-proxy-rules
3+
======
4+
`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.
5+
6+
## Installation
7+
```sh
8+
npm install http-proxy-rules --save
9+
```
10+
11+
## Example Use Case
12+
```js
13+
var http = require('http'),
14+
httpProxy = require('http-proxy'),
15+
HttpProxyRules = require('http-proxy-rules');
16+
17+
// Set up proxy rules instance
18+
var proxyRules = new HttpProxyRules({
19+
rules: {
20+
'.*/test': 'http://localhost:8080/cool',
21+
'.*/test2/': 'http://localhost:8080/cool2/'
22+
},
23+
default: 'http://localhost:8080'
24+
});
25+
26+
// Create reverse proxy instance
27+
var proxy = httpProxy.createProxy();
28+
29+
// Create http server that leverages reverse proxy instance
30+
// and proxy rules to proxy requests to different targets
31+
http.createServer(function(req, res) {
32+
33+
// a test method is exposed on the proxy rules instance
34+
// to test a request to see if it matches against one of the specified rules
35+
var target;
36+
if (target = proxyRules.test(req)) {
37+
return proxy.web(req, res, {
38+
target: target
39+
});
40+
}
41+
42+
res.writeHead(500, { 'Content-Type': 'text/plain' });
43+
res.end('The request url and path did not match any of the listed rules!');
44+
}).listen(6010, cb);
45+
```
46+
47+
## Options
48+
49+
You can initialize a new `http-proxy-rules` instance with the following options:
50+
51+
```js
52+
{
53+
rules: {}, // See notes below
54+
default: '' // (optional) if there are no matching rules, translate url path to the specified dfeualt
55+
}
56+
```
57+
The rules object contains a set of key-value pairs mapping a regex-supported url path to a target route. The target route must include the protocol (e.g., http) and the FQDN. See the [tests](test/index.tests.js) for examples of how incoming route url paths may be translated with the use of this module.
58+
59+
## Other Notes
60+
* `(?:\\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`.
61+
* 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.

example/simple.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var http = require('http'),
55

66
module.exports = function spawnReverseProxy(cb) {
77

8-
// Set up proxy rules object
8+
// Set up proxy rules instance
99
var proxyRules = new HttpProxyRules({
1010
rules: {
1111
'.*/test': 'http://localhost:8080/cool',
@@ -21,6 +21,8 @@ module.exports = function spawnReverseProxy(cb) {
2121
// and proxy rules to proxy requests to different targets
2222
http.createServer(function(req, res) {
2323

24+
// a test method is exposed on the proxy rules instance
25+
// to test a request to see if it matches against one of the specified rules
2426
var target;
2527
if (target = proxyRules.test(req)) {
2628
return proxy.web(req, res, {

0 commit comments

Comments
 (0)