Skip to content

Commit aa89e1f

Browse files
committed
chore(docs): improve docs (chimurai#65)
1 parent 0680fa3 commit aa89e1f

31 files changed

+718
-332
lines changed

README.md

Lines changed: 83 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@
33
[![Build Status](https://img.shields.io/travis/chimurai/http-proxy-middleware/master.svg?style=flat-square)](https://travis-ci.org/chimurai/http-proxy-middleware)
44
[![Coveralls](https://img.shields.io/coveralls/chimurai/http-proxy-middleware.svg?style=flat-square)](https://coveralls.io/r/chimurai/http-proxy-middleware)
55
[![dependency Status](https://img.shields.io/david/chimurai/http-proxy-middleware.svg?style=flat-square)](https://david-dm.org/chimurai/http-proxy-middleware#info=dependencies)
6-
[![devDependency Status](https://img.shields.io/david/dev/chimurai/http-proxy-middleware.svg?style=flat-square)](https://david-dm.org/chimurai/http-proxy-middleware#info=devDependencies)
76

8-
Node.js proxying made simple. Configure proxy middleware with ease for [connect](https://github.com/senchalabs/connect), [express](https://github.com/strongloop/express) and [browser-sync](https://github.com/BrowserSync/browser-sync).
7+
Node.js proxying made simple. Configure proxy middleware with ease for [connect](https://github.com/senchalabs/connect), [express](https://github.com/strongloop/express), [browser-sync](https://github.com/BrowserSync/browser-sync) and [many more](#compatible-servers).
98

109
Powered by the popular Nodejitsu [`http-proxy`](https://github.com/nodejitsu/node-http-proxy). [![GitHub stars](https://img.shields.io/github/stars/nodejitsu/node-http-proxy.svg?style=social&label=Star)](https://github.com/nodejitsu/node-http-proxy)
1110

11+
## Table of Contents
12+
13+
<!-- MarkdownTOC autolink=true bracket=round depth=2 -->
14+
15+
- [Install](#install)
16+
- [Core concept](#core-concept)
17+
- [Example](#example)
18+
- [Context matching](#context-matching)
19+
- [Shorthand](#shorthand)
20+
- [WebSocket](#websocket)
21+
- [Options](#options)
22+
- [Working examples](#working-examples)
23+
- [Recipes](#recipes)
24+
- [Compatible servers](#compatible-servers)
25+
- [Tests](#tests)
26+
- [Changelog](#changelog)
27+
- [License](#license)
28+
29+
<!-- /MarkdownTOC -->
30+
31+
1232
## Install
1333

1434
```javascript
@@ -19,38 +39,39 @@ $ npm install --save-dev http-proxy-middleware
1939

2040
Configure the proxy middleware.
2141
```javascript
22-
var proxyMiddleware = require('http-proxy-middleware');
42+
var proxy = require('http-proxy-middleware');
2343

24-
var proxy = proxyMiddleware('/api', {target: 'http://www.example.org'});
25-
// \____/ \________________________________/
26-
// | |
27-
// context options
44+
var apiProxy = proxy('/api', {target: 'http://www.example.org'});
45+
// \____/ \_____________________________/
46+
// | |
47+
// context options
2848

29-
// 'proxy' is now ready to be used in a server.
49+
// 'apiProxy' is now ready to be used as middleware in a server.
3050
```
3151
* **context**: matches provided context against request-urls' **path**.
3252
Matching requests will be proxied to the target host.
3353
Example: `'/api'` or `['/api', '/ajax']`. (more about [context matching](#context-matching))
34-
* **options.target**: target host to proxy to.
35-
Check out available [proxy middleware options](#options).
54+
* **options.target**: target host to proxy to. (full list of [proxy middleware options](#options))
3655

3756
``` javascript
3857
// shorthand syntax for the example above:
39-
var proxy = proxyMiddleware('http://www.example.org/api');
58+
var apiProxy = proxy('http://www.example.org/api');
4059

4160
```
4261
More about the [shorthand configuration](#shorthand).
4362

4463
## Example
4564

46-
An example with express server.
65+
An example with `express` server.
66+
4767
```javascript
4868
// include dependencies
4969
var express = require('express');
50-
var proxyMiddleware = require('http-proxy-middleware');
70+
var proxy = require('http-proxy-middleware');
5171

5272
// configure proxy middleware context
5373
var context = '/api'; // requests with this path will be proxied
74+
// use Array for multipath: ['/api', '/rest']
5475

5576
// configure proxy middleware options
5677
var options = {
@@ -69,21 +90,19 @@ var options = {
6990
};
7091

7192
// create the proxy
72-
var proxy = proxyMiddleware(context, options);
93+
var apiProxy = proxy(context, options);
7394

74-
// use the configured `proxy` in web server
95+
// use the configured `apiProxy` in web server
7596
var app = express();
76-
app.use(proxy);
97+
app.use(apiProxy);
7798
app.listen(3000);
7899
```
79100

80-
Check out [working examples](#more-examples).
81-
82101
**Tip:** For [name-based virtual hosted sites](http://en.wikipedia.org/wiki/Virtual_hosting#Name-based), you'll need to use the option `changeOrigin` and set it to `true`.
83102

84103
## Context matching
85104

86-
http-proxy-middleware offers several ways to decide which requests should be proxied.
105+
`http-proxy-middleware` offers several ways to decide which requests should be proxied.
87106
Request URL's [ _path-absolute_ and _query_](https://tools.ietf.org/html/rfc3986#section-3) will be used for context matching .
88107

89108
* **path matching**
@@ -111,50 +130,50 @@ Request URL's [ _path-absolute_ and _query_](https://tools.ietf.org/html/rfc3986
111130
return (path.match('^/api') && req.method === 'GET');
112131
};
113132

114-
var apiProxy = proxyMiddleware(filter, {target: 'http://www.example.org'})
133+
var apiProxy = proxy(filter, {target: 'http://www.example.org'})
115134
```
116135

117136
## Shorthand
118137

119138
Use the shorthand syntax when verbose configuration is not needed. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.
120139

121140
```javascript
122-
proxyMiddleware('http://www.example.org:8000/api');
123-
// proxyMiddleware('/api', {target: 'http://www.example.org:8000'});
141+
proxy('http://www.example.org:8000/api');
142+
// proxy('/api', {target: 'http://www.example.org:8000'});
124143
125144
126-
proxyMiddleware('http://www.example.org:8000/api/books/*/**.json');
127-
// proxyMiddleware('/api/books/*/**.json', {target: 'http://www.example.org:8000'});
145+
proxy('http://www.example.org:8000/api/books/*/**.json');
146+
// proxy('/api/books/*/**.json', {target: 'http://www.example.org:8000'});
128147
129148
130-
proxyMiddleware('http://www.example.org:8000/api', {changeOrigin:true});
131-
// proxyMiddleware('/api', {target: 'http://www.example.org:8000', changeOrigin: true});
149+
proxy('http://www.example.org:8000/api', {changeOrigin:true});
150+
// proxy('/api', {target: 'http://www.example.org:8000', changeOrigin: true});
132151
```
133152

134153
## WebSocket
135154

136155
```javascript
137156
// verbose api
138-
proxyMiddleware('/', {target:'http://echo.websocket.org', ws:true});
157+
proxy('/', {target:'http://echo.websocket.org', ws:true});
139158
140159
// shorthand
141-
proxyMiddleware('http://echo.websocket.org', {ws:true});
160+
proxy('http://echo.websocket.org', {ws:true});
142161
143162
// shorter shorthand
144-
proxyMiddleware('ws://echo.websocket.org');
163+
proxy('ws://echo.websocket.org');
145164
```
146165

147166
### External WebSocket upgrade
148167

149168
In the previous WebSocket examples, http-proxy-middleware relies on a initial http request in order to listen to the http `upgrade` event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http `upgrade` event manually.
150169
```javascript
151-
var proxy = proxyMiddleware('ws://echo.websocket.org', {changeOrigin:true});
170+
var wsProxy = proxy('ws://echo.websocket.org', {changeOrigin:true});
152171

153172
var app = express();
154-
app.use(proxy);
173+
app.use(wsProxy);
155174

156175
var server = app.listen(3000);
157-
server.on('upgrade', proxy.upgrade); // <-- subscribe to http 'upgrade'
176+
server.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'
158177
```
159178

160179
## Options
@@ -174,14 +193,14 @@ var server = app.listen(3000);
174193
* **option.proxyTable**: object, re-target `option.target` based on the request header `host` parameter. `host` can be used in conjunction with `path`. Only one instance of the proxy will be used. The order of the configuration matters.
175194
```javascript
176195
proxyTable: {
177-
"integration.localhost:3000" : "http://localhost:8001", // host only
178-
"staging.localhost:3000" : "http://localhost:8002", // host only
179-
"localhost:3000/api" : "http://localhost:8003", // host + path
180-
"/rest" : "http://localhost:8004" // path only
196+
"integration.localhost:3000" : "http://localhost:8001", // host only
197+
"staging.localhost:3000" : "http://localhost:8002", // host only
198+
"localhost:3000/api" : "http://localhost:8003", // host + path
199+
"/rest" : "http://localhost:8004" // path only
181200
}
182201
```
183202

184-
* **option.logLevel**: string, ['debug', 'info', 'warn', 'error', 'silent']. Default: 'info'
203+
* **option.logLevel**: string, ['debug', 'info', 'warn', 'error', 'silent']. Default: `'info'`
185204

186205
* **option.logProvider**: function, modify or replace log provider. Default: `console`.
187206
```javascript
@@ -270,7 +289,7 @@ The following options are provided by the underlying [http-proxy](https://www.np
270289
* **option.ws**: true/false: if you want to proxy websockets
271290
* **option.xfwd**: true/false, adds x-forward headers
272291
* **option.secure**: true/false, if you want to verify the SSL Certs
273-
* **option.toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies)
292+
* **option.toProxy**: true/false, passes the absolute URL as the `path` (useful for proxying to proxies)
274293
* **option.prependPath**: true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
275294
* **option.ignorePath**: true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
276295
* **option.localAddress** : Local interface string to bind for outgoing connections
@@ -281,50 +300,52 @@ The following options are provided by the underlying [http-proxy](https://www.np
281300
* **option.protocolRewrite**: rewrites the location protocol on (301/302/307/308) redirects to 'http' or 'https'. Default: null.
282301
* **option.headers**: object, adds [request headers](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields). (Example: `{host:'www.example.org'}`)
283302

284-
## Recipes
285-
286-
View the [recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes) for common use cases.
303+
## Working examples
287304

288-
## More Examples
289-
290-
To run and view the [proxy examples](https://github.com/chimurai/http-proxy-middleware/tree/master/examples), clone the http-proxy-middleware repo and install the dependencies:
291-
292-
```bash
293-
$ git clone https://github.com/chimurai/http-proxy-middleware.git
294-
$ cd http-proxy-middleware
295-
$ npm install
296-
```
305+
View and play around with [working examples](https://github.com/chimurai/http-proxy-middleware/tree/master/examples).
297306

298-
Run the example:
307+
* Browser-Sync ([exampe source](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js))
308+
* express ([exampe source](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js))
309+
* connect ([exampe source](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js))
310+
* WebSocket ([exampe source](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js))
299311

300-
```bash
301-
$ node examples/connect
302-
```
312+
## Recipes
303313

304-
Or just explore the proxy examples' sources:
305-
* `examples/connect` - [connect proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js)
306-
* `examples/express` - [express proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js)
307-
* `examples/browser-sync` - [browser-sync proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js)
308-
* `examples/websocket` - [websocket proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js) with express
314+
View the [recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes) for common use cases.
309315

310316
## Compatible servers
311317

312-
http-proxy-middleware is compatible with the following servers:
318+
`http-proxy-middleware` is compatible with the following servers:
313319
* [connect](https://www.npmjs.com/package/connect)
314320
* [express](https://www.npmjs.com/package/express)
315321
* [browser-sync](https://www.npmjs.com/package/browser-sync)
322+
* [lite-server](https://www.npmjs.com/package/lite-server)
323+
* [grunt-contrib-connect](https://www.npmjs.com/package/grunt-contrib-connect)
324+
* [grunt-browser-sync](https://www.npmjs.com/package/grunt-browser-sync)
325+
* [gulp-connect](https://www.npmjs.com/package/gulp-connect)
326+
* [gulp-webserver](https://www.npmjs.com/package/gulp-webserver)
327+
328+
Sample implementations can be found in the [server recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes/servers.md).
316329

317330
## Tests
318331

319-
To run the test suite, first install the dependencies, then run:
332+
Run the test suite:
320333

321334
```bash
322335
# install dependencies
323336
$ npm install
337+
```
338+
339+
unit testing
324340

341+
```bash
325342
# unit tests
326343
$ npm test
344+
```
345+
346+
coverage
327347

348+
```bash
328349
# code coverage
329350
$ npm run cover
330351
```
@@ -338,4 +359,4 @@ $ npm run cover
338359

339360
The MIT License (MIT)
340361

341-
Copyright (c) 2015 Steven Chim
362+
Copyright (c) 2015-2016 Steven Chim

examples/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Examples
2+
3+
View working examples of `http-proxy-middleware` implemented in popular servers.
4+
5+
To run and view the [examples](https://github.com/chimurai/http-proxy-middleware/tree/master/examples); Clone the `http-proxy-middleware` repo and install the dependencies:
6+
7+
```bash
8+
$ git clone https://github.com/chimurai/http-proxy-middleware.git
9+
$ cd http-proxy-middleware
10+
$ npm install
11+
```
12+
13+
Run the example from root folder:
14+
15+
```bash
16+
$ node examples/browser-sync
17+
```
18+
19+
```bash
20+
$ node examples/connect
21+
```
22+
23+
```bash
24+
$ node examples/express
25+
```
26+
27+
```bash
28+
$ node examples/websocket
29+
```
30+
31+
## Server recipes
32+
33+
You can find more server implementations with `http-proxy-middleware` in the [server recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes/servers.md)

examples/browser-sync/index.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
/**
22
* Module dependencies.
33
*/
4-
var browserSync = require('../../node_modules/browser-sync/index').create(); // require('browser-sync').create();
5-
var proxyMiddleware = require('../../index'); // require('http-proxy-middleware');
4+
var browserSync = require('browser-sync').create();
5+
var proxy = require('../../index'); // require('http-proxy-middleware');
66

7-
// configure proxy middleware
8-
// context: '/' will proxy all requests
9-
// use: '/api' to proxy request when path starts with '/api'
10-
var proxy = proxyMiddleware('/api', {
11-
target: 'http://www.example.org',
12-
changeOrigin: true // for vhosted sites, changes host header to match to target's host
13-
});
7+
/**
8+
* Configure proxy middleware
9+
*/
10+
var chuckNorrisApiProxy = proxy('/jokes', {
11+
target: 'http://api.icndb.com',
12+
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
13+
logLevel: 'debug'
14+
});
1415

16+
/**
17+
* Add the proxy to browser-sync
18+
*/
1519
browserSync.init({
1620
server: {
1721
baseDir: './',
1822
port: 3000,
19-
middleware: [proxy], // add the proxy to browser-sync
23+
middleware: [chuckNorrisApiProxy],
2024
},
21-
startPath: '/api'
25+
startPath: '/jokes/random/5?limitTo=[nerdy]'
2226
});
2327

24-
console.log('listening on port 3000');
25-
console.log('try:');
26-
console.log(' http://localhost:3000/api');
28+
console.log('[DEMO] Server: listening on port 3000');
29+
console.log('[DEMO] Opening: http://localhost:3000/jokes/random/5?limitTo=[nerdy]');

0 commit comments

Comments
 (0)