http-proxy-middleware 0.17.0 → 0.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +21 -6
  3. package/lib/handlers.js +13 -1
  4. package/lib/index.js +33 -10
  5. package/lib/logger.js +2 -2
  6. package/package.json +16 -12
  7. package/.editorconfig +0 -18
  8. package/.jscsrc +0 -8
  9. package/.npmignore +0 -28
  10. package/.travis.yml +0 -16
  11. package/CONTRIBUTING.md +0 -61
  12. package/ISSUE_TEMPLATE.md +0 -21
  13. package/examples/README.md +0 -33
  14. package/examples/browser-sync/index.js +0 -29
  15. package/examples/connect/index.js +0 -29
  16. package/examples/express/index.js +0 -28
  17. package/examples/websocket/index.html +0 -96
  18. package/examples/websocket/index.js +0 -41
  19. package/recipes/README.md +0 -110
  20. package/recipes/basic.md +0 -32
  21. package/recipes/context-matching.md +0 -99
  22. package/recipes/corporate-proxy.md +0 -21
  23. package/recipes/delay.md +0 -37
  24. package/recipes/logLevel.md +0 -40
  25. package/recipes/logProvider.md +0 -77
  26. package/recipes/modify-post.md +0 -74
  27. package/recipes/pathRewrite.md +0 -93
  28. package/recipes/proxy-events.md +0 -74
  29. package/recipes/router.md +0 -83
  30. package/recipes/servers.md +0 -252
  31. package/recipes/shorthand.md +0 -63
  32. package/recipes/virtual-hosts.md +0 -18
  33. package/recipes/websocket.md +0 -41
  34. package/test/e2e/_utils.js +0 -21
  35. package/test/e2e/http-proxy-middleware.spec.js +0 -672
  36. package/test/e2e/path-rewriter.spec.js +0 -98
  37. package/test/e2e/router.spec.js +0 -128
  38. package/test/e2e/websocket.spec.js +0 -150
  39. package/test/unit/_libs.js +0 -8
  40. package/test/unit/config-factory.spec.js +0 -154
  41. package/test/unit/context-matcher.spec.js +0 -250
  42. package/test/unit/handlers.spec.js +0 -126
  43. package/test/unit/logger.spec.js +0 -259
  44. package/test/unit/path-rewriter.spec.js +0 -143
  45. package/test/unit/router.spec.js +0 -136
@@ -1,74 +0,0 @@
1
- # Proxy Events
2
-
3
- Subscribe to [`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) events: `error`, `proxyReq`, `proxyReqWs`, `proxyRes`, `open`, `close`.
4
-
5
- ## onError
6
-
7
- Subscribe to http-proxy's [error event](https://www.npmjs.com/package/http-proxy#listening-for-proxy-events).
8
-
9
- ```javascript
10
- var proxy = require("http-proxy-middleware");
11
-
12
- var onError = function (err, req, res) {
13
- console.log('Something went wrong.');
14
- console.log('And we are reporting a custom error message.');
15
- };
16
-
17
- var options = {target:'http://localhost:3000', onError: onError};
18
-
19
- var apiProxy = proxy('/api', options);
20
- ```
21
-
22
- ## onProxyReq
23
-
24
- Subscribe to http-proxy's [proxyReq event](https://www.npmjs.com/package/http-proxy#listening-for-proxy-events).
25
-
26
- ```javascript
27
- var proxy = require("http-proxy-middleware");
28
-
29
- var onProxyReq = function (proxyReq, req, res) {
30
- // add new header to request
31
- proxyReq.setHeader('x-added', 'foobar');
32
- };
33
-
34
- var options = {target:'http://localhost:3000', onProxyReq: onProxyReq};
35
-
36
- var apiProxy = proxy('/api', options);
37
- ```
38
-
39
- ## onProxyReqWs
40
-
41
- Subscribe to http-proxy's [proxyReqWs event](https://www.npmjs.com/package/http-proxy#listening-for-proxy-events).
42
-
43
- ```javascript
44
- var proxy = require("http-proxy-middleware");
45
-
46
- var onProxyReqWs = function (proxyReq, req, socket, options, head) {
47
- // add custom header
48
- proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
49
- };
50
-
51
- var options = {target:'http://localhost:3000', onProxyReq: onProxyReqWs};
52
-
53
- var apiProxy = proxy('/api', options);
54
- ```
55
-
56
- ## onProxyRes
57
-
58
- Subscribe to http-proxy's [proxyRes event](https://www.npmjs.com/package/http-proxy#listening-for-proxy-events).
59
-
60
- ```javascript
61
- var proxy = require("http-proxy-middleware");
62
-
63
- var onProxyRes = function (proxyRes, req, res) {
64
- // add new header to response
65
- proxyRes.headers['x-added'] = 'foobar';
66
-
67
- // remove header from response
68
- delete proxyRes.headers['x-removed'];
69
- };
70
-
71
- var options = {target:'http://localhost:3000', onProxyRes: onProxyRes};
72
-
73
- var apiProxy = proxy('/api', options);
74
- ```
package/recipes/router.md DELETED
@@ -1,83 +0,0 @@
1
- # router
2
-
3
- Allows you to route to a different `target` by using a table of a custom function.
4
-
5
- <!-- MarkdownTOC autolink=true bracket=round -->
6
-
7
- - [Custom router function](#custom-router-function)
8
- - [Proxy Table](#proxy-table)
9
-
10
- <!-- /MarkdownTOC -->
11
-
12
-
13
- ## Custom router function
14
-
15
- Write your own router to dynamically route to a different `target`.
16
- The `req` object is provided to retrieve contextual data.
17
-
18
- ```javascript
19
- var express = require('express');
20
- var proxy = require("http-proxy-middleware");
21
-
22
- var customRouter = function(req) {
23
- return 'http://www.example.org' // protocol + host
24
- };
25
-
26
- var options = {
27
- target: 'http://localhost:8000',
28
- router: customRouter
29
- };
30
-
31
- var myProxy = proxy(options);
32
-
33
- var app = express();
34
- app.use(myProxy); // add the proxy to express
35
-
36
- app.listen(3000);
37
- ```
38
-
39
-
40
- ## Proxy Table
41
-
42
- Use a Proxy Table to proxy requests to a different `target` based on:
43
- * Host [HTTP header](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields).
44
- * Request path
45
- * Host HTTP header + path
46
-
47
- ```javascript
48
- var express = require('express');
49
- var proxy = require("http-proxy-middleware");
50
-
51
- var proxyTable = {
52
- "integration.localhost:3000" : "http://localhost:8001", // host only
53
- "staging.localhost:3000" : "http://localhost:8002", // host only
54
- "localhost:3000/api" : "http://localhost:8003", // host + path
55
- "/rest" : "http://localhost:8004" // path only
56
- };
57
-
58
- var options = {
59
- target: 'http://localhost:8000',
60
- router: proxyTable
61
- };
62
-
63
- var myProxy = proxy(options);
64
-
65
- var app = express();
66
- app.use(myProxy); // add the proxy to express
67
-
68
- app.listen(3000);
69
- ```
70
-
71
- ### Example
72
-
73
- In the example above; all requests will be proxied to `http://localhost:8000`.
74
-
75
- When request's `Host HTTP header` and/or `path` match a configuration in the proxyTable, they will be send to matching target.
76
-
77
- ```
78
- http://localhost:3000/lorum/ipsum -> http://localhost:8000/lorum/ipsum
79
- http://integration.localhost:3000/lorum/ipsum -> http://localhost:8001/lorum/ipsum
80
- http://staging.localhost:3000/rest/foo/bar -> http://localhost:8002/rest/foo/bar
81
- http://localhost:3000/api/houses/123 -> http://localhost:8003/api/houses/123
82
- http://localhost:3000/rest/books/123 -> http://localhost:8004/rest/books/123
83
- ```
@@ -1,252 +0,0 @@
1
- # Servers
2
-
3
- Overview of `http-proxy-middleware` implementation in different servers.
4
-
5
- Missing a server? Feel free to extend this list of examples.
6
-
7
- <!-- MarkdownTOC autolink=true bracket=round -->
8
-
9
- - [Browser-Sync](#browser-sync)
10
- - [Express](#express)
11
- - [Connect](#connect)
12
- - [lite-server](#lite-server)
13
- - [grunt-contrib-connect](#grunt-contrib-connect)
14
- - [grunt-browser-sync](#grunt-browser-sync)
15
- - [gulp-connect](#gulp-connect)
16
- - [gulp-webserver](#gulp-webserver)
17
-
18
- <!-- /MarkdownTOC -->
19
-
20
- ## Browser-Sync
21
-
22
- https://github.com/BrowserSync/browser-sync
23
- [![GitHub stars](https://img.shields.io/github/stars/BrowserSync/browser-sync.svg?style=social&label=Star)](https://github.com/BrowserSync/browser-sync)
24
-
25
- ```javascript
26
- var browserSync = require('browser-sync').create();
27
- var proxy = require('http-proxy-middleware');
28
-
29
- var apiProxy = proxy('/api', {
30
- target: 'http://www.example.org',
31
- changeOrigin: true // for vhosted sites
32
- });
33
-
34
- browserSync.init({
35
- server: {
36
- baseDir: './',
37
- port: 3000,
38
- middleware: [apiProxy],
39
- },
40
- startPath: '/api'
41
- });
42
-
43
- ```
44
-
45
- ## Express
46
-
47
- https://github.com/expressjs/express
48
- [![GitHub stars](https://img.shields.io/github/stars/expressjs/express.svg?style=social&label=Star)](https://github.com/expressjs/express)
49
-
50
- ```javascript
51
- var express = require('express');
52
- var proxy = require('http-proxy-middleware');
53
-
54
- var apiProxy = proxy('/api', {
55
- target: 'http://www.example.org',
56
- changeOrigin: true // for vhosted sites
57
- });
58
-
59
- var app = express();
60
-
61
- app.use(apiProxy);
62
- app.listen(3000);
63
- ```
64
-
65
- ## Connect
66
-
67
- https://github.com/senchalabs/connect
68
- [![GitHub stars](https://img.shields.io/github/stars/senchalabs/connect.svg?style=social&label=Star)](https://github.com/senchalabs/connect)
69
-
70
- ```javascript
71
- var http = require('http');
72
- var connect = require('connect');
73
- var proxy = require('http-proxy-middleware');
74
-
75
- var apiProxy = proxy('/api', {
76
- target: 'http://www.example.org',
77
- changeOrigin: true // for vhosted sites
78
- });
79
-
80
- var app = connect();
81
- app.use(apiProxy);
82
-
83
- http.createServer(app).listen(3000);
84
- ```
85
-
86
- ## lite-server
87
-
88
- https://github.com/johnpapa/lite-server
89
- [![GitHub stars](https://img.shields.io/github/stars/johnpapa/lite-server.svg?style=social&label=Star)](https://github.com/johnpapa/lite-server) ([example source](https://github.com/johnpapa/lite-server/issues/61#issuecomment-205997607))
90
-
91
- File: `bs-config.js`
92
-
93
- ```javascript
94
- var proxy = require('http-proxy-middleware');
95
-
96
- var apiProxy = proxy('/api', {
97
- target: 'http://www.example.org',
98
- changeOrigin: true // for vhosted sites
99
- });
100
-
101
- module.exports = {
102
- server: {
103
- middleware: {
104
- 1: apiProxy
105
- }
106
- }
107
- };
108
- ```
109
-
110
- ## grunt-contrib-connect
111
-
112
- https://github.com/gruntjs/grunt-contrib-connect
113
- [![GitHub stars](https://img.shields.io/github/stars/gruntjs/grunt-contrib-connect.svg?style=social&label=Star)](https://github.com/gruntjs/grunt-contrib-connect)
114
-
115
- As an `Array`:
116
- ```javascript
117
- var proxy = require('http-proxy-middleware');
118
-
119
- var apiProxy = proxy('/api', {
120
- target: 'http://www.example.org',
121
- changeOrigin: true // for vhosted sites
122
- });
123
-
124
- grunt.initConfig({
125
- connect: {
126
- server: {
127
- options: {
128
- middleware: [apiProxy],
129
- },
130
- },
131
- },
132
- });
133
- ```
134
-
135
- As a `function`:
136
- ```javascript
137
- var proxy = require('http-proxy-middleware');
138
-
139
- var apiProxy = proxy('/api', {
140
- target: 'http://www.example.org',
141
- changeOrigin: true // for vhosted sites
142
- });
143
-
144
- grunt.initConfig({
145
- connect: {
146
- server: {
147
- options: {
148
- middleware: function(connect, options, middlewares) {
149
- // inject a custom middleware into the array of default middlewares
150
- middlewares.unshift(apiProxy);
151
-
152
- return middlewares;
153
- },
154
- },
155
- },
156
- },
157
- });
158
- ```
159
-
160
-
161
- ## grunt-browser-sync
162
-
163
- https://github.com/BrowserSync/grunt-browser-sync
164
- [![GitHub stars](https://img.shields.io/github/stars/BrowserSync/grunt-browser-sync.svg?style=social&label=Star)](https://github.com/BrowserSync/grunt-browser-sync)
165
-
166
-
167
- ```javascript
168
- var proxy = require('http-proxy-middleware');
169
-
170
- var apiProxy = proxy('/api', {
171
- target: 'http://www.example.org',
172
- changeOrigin: true // for vhosted sites
173
- });
174
-
175
- grunt.initConfig({
176
-
177
- // BrowserSync Task
178
- browserSync: {
179
- default_options: {
180
- options: {
181
- files: [
182
- "css/*.css",
183
- "*.html"
184
- ],
185
- port: 9000,
186
- server: {
187
- baseDir: ['app'],
188
- middleware: apiProxy
189
- }
190
- }
191
- }
192
- }
193
-
194
- });
195
- ```
196
-
197
- ## gulp-connect
198
-
199
- https://github.com/avevlad/gulp-connect
200
- [![GitHub stars](https://img.shields.io/github/stars/avevlad/gulp-connect.svg?style=social&label=Star)](https://github.com/avevlad/gulp-connect)
201
-
202
- ```javascript
203
- var gulp = require('gulp');
204
- var connect = require('gulp-connect');
205
- var proxy = require('http-proxy-middleware');
206
-
207
- gulp.task('connect', function() {
208
- connect.server({
209
- root: ['./app'],
210
- middleware: function(connect, opt) {
211
-
212
- var apiProxy = proxy('/api', {
213
- target: 'http://www.example.org',
214
- changeOrigin: true // for vhosted sites
215
- });
216
-
217
- return [apiProxy];
218
- }
219
-
220
- });
221
- });
222
-
223
- gulp.task('default', ['connect']);
224
- ```
225
-
226
- ## gulp-webserver
227
-
228
- https://github.com/schickling/gulp-webserver
229
- [![GitHub stars](https://img.shields.io/github/stars/schickling/gulp-webserver.svg?style=social&label=Star)](https://github.com/schickling/gulp-webserver)
230
-
231
- ```javascript
232
- var gulp = require('gulp');
233
- var webserver = require('gulp-webserver');
234
- var proxy = require('http-proxy-middleware');
235
-
236
- gulp.task('webserver', function() {
237
- var apiProxy = proxy('/api', {
238
- target: 'http://www.example.org',
239
- changeOrigin: true // for vhosted sites
240
- });
241
-
242
- gulp.src('app')
243
- .pipe(webserver({
244
- livereload: true,
245
- directoryListing: true,
246
- open: true,
247
- middleware: [apiProxy]
248
- }));
249
- });
250
-
251
- gulp.task('default', ['webserver']);
252
- ```
@@ -1,63 +0,0 @@
1
- # Shorthand
2
-
3
- This example will create a proxy middleware using the shorthand notation.
4
-
5
- The http-proxy-middleware `context` and `config.target` will be set automatically.
6
-
7
- ```javascript
8
- var proxy = require("http-proxy-middleware");
9
-
10
- var apiProxy = proxy('http://localhost:3000/api');
11
-
12
- // equivalent:
13
- // var apiProxy = proxy('/api', {target:'http://localhost:3000'});
14
- ```
15
-
16
- ## Shorthand - Wildcard context
17
-
18
- This example will create a proxy middleware with shorthand wildcard context.
19
-
20
- ```javascript
21
- var proxy = require("http-proxy-middleware");
22
-
23
- var apiProxy = proxy('http://localhost:3000/api/books/*/**.json');
24
- // equals:
25
- // var apiProxy = proxy('/api/books/*/**.json', {target:'http://localhost:3000'});
26
- ```
27
-
28
-
29
- ## Shorthand with additional configuration
30
-
31
- This example will create a proxy middleware with shorthand and additional configuration.
32
-
33
- ```javascript
34
- var proxy = require("http-proxy-middleware");
35
-
36
- var apiProxy = proxy('http://localhost:3000/api', {changeOrigin: true});
37
- // equals:
38
- // var apiProxy = proxy('/api', {target:'http://localhost:3000', {changeOrigin:true}});
39
- ```
40
-
41
- ## Shorthand - WebSocket
42
-
43
- This example will create a proxy middleware with shorthand and additional configuration for WebSocket support.
44
-
45
- ```javascript
46
- var proxy = require("http-proxy-middleware");
47
-
48
- var apiProxy = proxy('http://localhost:3000/api', {ws: true});
49
- // equals:
50
- // var apiProxy = proxy('/api', {target:'http://localhost:3000', ws: true});
51
- ```
52
-
53
- ## Shorthand - WebSocket only
54
-
55
- This example will create a proxy middleware with websocket shorthand only configuration.
56
-
57
- ```javascript
58
- var proxy = require("http-proxy-middleware");
59
-
60
- var apiProxy = proxy('ws://localhost:3000/api');
61
- // equals:
62
- // var apiProxy = proxy('/api', {target:'ws://localhost:3000', ws: true});
63
- ```
@@ -1,18 +0,0 @@
1
- # Name-based Virtual Hosts
2
-
3
- This example will create a basic proxy middleware for [virtual hosted sites](https://en.wikipedia.org/wiki/Virtual_hosting#Name-based).
4
-
5
- When `changeOrigin` is set to `true`; Host [HTTP header](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields) will be set to match target's host.
6
-
7
- The `changeOrigin` option is provided by [http-proxy](https://github.com/nodejitsu/node-http-proxy).
8
-
9
- ```javascript
10
- var proxy = require("http-proxy-middleware");
11
-
12
- var options = {
13
- target: 'http://localhost:3000',
14
- changeOrigin:true
15
- };
16
-
17
- var apiProxy = proxy('/api', options);
18
- ```
@@ -1,41 +0,0 @@
1
- # WebSocket
2
-
3
- This example will create a proxy middleware with websocket support.
4
-
5
- ```javascript
6
- var proxy = require("http-proxy-middleware");
7
-
8
- var socketProxy = proxy('/socket', {target: 'http://localhost:3000', ws: true});
9
- ```
10
-
11
- ## WebSocket - Path Rewrite
12
-
13
- This example will create a proxy middleware with websocket support and pathRewrite.
14
-
15
- ```javascript
16
- var proxy = require("http-proxy-middleware");
17
-
18
- var options = {
19
- target: 'http://localhost:3000',
20
- ws: true,
21
- pathRewrite: {
22
- '^/socket' : ''
23
- }
24
- };
25
-
26
- var socketProxy = proxy('/socket', options);
27
- ```
28
-
29
- ## WebSocket - Server update subscription
30
-
31
- This example will create a proxy middleware with websocket support.
32
-
33
- Subscribe to server's upgrade event.
34
-
35
- ```javascript
36
- var proxy = require("http-proxy-middleware");
37
-
38
- var socketProxy = proxy('/socket', {target: 'http://localhost:3000', ws: true});
39
-
40
- server.on('upgrade', proxy.upgrade); // <-- subscribe to http 'upgrade'
41
- ```
@@ -1,21 +0,0 @@
1
- var express = require('express');
2
- var proxyMiddleware = require('../../index');
3
-
4
- module.exports = {
5
- createServer: createServer,
6
- proxyMiddleware: proxyMiddleware
7
- };
8
-
9
- function createServer(portNumber, middleware, path) {
10
- var app = express();
11
-
12
- if (middleware, path) {
13
- app.use(path, middleware);
14
- } else if (middleware) {
15
- app.use(middleware);
16
- }
17
-
18
- var server = app.listen(portNumber);
19
-
20
- return server;
21
- }