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.
- package/CHANGELOG.md +12 -0
- package/README.md +21 -6
- package/lib/handlers.js +13 -1
- package/lib/index.js +33 -10
- package/lib/logger.js +2 -2
- package/package.json +16 -12
- package/.editorconfig +0 -18
- package/.jscsrc +0 -8
- package/.npmignore +0 -28
- package/.travis.yml +0 -16
- package/CONTRIBUTING.md +0 -61
- package/ISSUE_TEMPLATE.md +0 -21
- package/examples/README.md +0 -33
- package/examples/browser-sync/index.js +0 -29
- package/examples/connect/index.js +0 -29
- package/examples/express/index.js +0 -28
- package/examples/websocket/index.html +0 -96
- package/examples/websocket/index.js +0 -41
- package/recipes/README.md +0 -110
- package/recipes/basic.md +0 -32
- package/recipes/context-matching.md +0 -99
- package/recipes/corporate-proxy.md +0 -21
- package/recipes/delay.md +0 -37
- package/recipes/logLevel.md +0 -40
- package/recipes/logProvider.md +0 -77
- package/recipes/modify-post.md +0 -74
- package/recipes/pathRewrite.md +0 -93
- package/recipes/proxy-events.md +0 -74
- package/recipes/router.md +0 -83
- package/recipes/servers.md +0 -252
- package/recipes/shorthand.md +0 -63
- package/recipes/virtual-hosts.md +0 -18
- package/recipes/websocket.md +0 -41
- package/test/e2e/_utils.js +0 -21
- package/test/e2e/http-proxy-middleware.spec.js +0 -672
- package/test/e2e/path-rewriter.spec.js +0 -98
- package/test/e2e/router.spec.js +0 -128
- package/test/e2e/websocket.spec.js +0 -150
- package/test/unit/_libs.js +0 -8
- package/test/unit/config-factory.spec.js +0 -154
- package/test/unit/context-matcher.spec.js +0 -250
- package/test/unit/handlers.spec.js +0 -126
- package/test/unit/logger.spec.js +0 -259
- package/test/unit/path-rewriter.spec.js +0 -143
- package/test/unit/router.spec.js +0 -136
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module dependencies.
|
|
3
|
-
*/
|
|
4
|
-
var express = require('express');
|
|
5
|
-
var proxy = require('../../index'); // require('http-proxy-middleware');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Configure proxy middleware
|
|
9
|
-
*/
|
|
10
|
-
var wsProxy = proxy('/', {
|
|
11
|
-
target: 'http://echo.websocket.org',
|
|
12
|
-
// pathRewrite: {
|
|
13
|
-
// '^/websocket' : '/socket', // rewrite path.
|
|
14
|
-
// '^/removepath' : '' // remove path.
|
|
15
|
-
// },
|
|
16
|
-
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
|
|
17
|
-
ws: true, // enable websocket proxy
|
|
18
|
-
logLevel: 'debug'
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
var app = express();
|
|
22
|
-
app.use('/', express.static(__dirname)); // demo page
|
|
23
|
-
app.use(wsProxy); // add the proxy to express
|
|
24
|
-
|
|
25
|
-
var server = app.listen(3000);
|
|
26
|
-
server.on('upgrade', wsProxy.upgrade); // optional: upgrade externally
|
|
27
|
-
|
|
28
|
-
console.log('[DEMO] Server: listening on port 3000');
|
|
29
|
-
console.log('[DEMO] Opening: http://localhost:3000');
|
|
30
|
-
|
|
31
|
-
require('opn')('http://localhost:3000');
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Example:
|
|
35
|
-
* Open http://localhost:3000 in WebSocket compatible browser.
|
|
36
|
-
* In browser console:
|
|
37
|
-
* 1. `var socket = new WebSocket('ws://localhost:3000');` // create new WebSocket
|
|
38
|
-
* 2. `socket.onmessage = function (msg) {console.log(msg)};` // listen to socket messages
|
|
39
|
-
* 3. `socket.send('hello world');` // send message
|
|
40
|
-
* > {data: "hello world"} // server should echo back your message.
|
|
41
|
-
**/
|
package/recipes/README.md
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
# Recipes
|
|
2
|
-
|
|
3
|
-
Common usages of `http-proxy-middleware`.
|
|
4
|
-
|
|
5
|
-
# Configuration example
|
|
6
|
-
|
|
7
|
-
Overview of `http-proxy-middleware` specific options.
|
|
8
|
-
|
|
9
|
-
http-proxy-middleware uses Nodejitsu's [http-proxy](https://github.com/nodejitsu/node-http-proxy) to do the actual proxying. All of its [options](https://github.com/nodejitsu/node-http-proxy#options) are exposed via http-proxy-middleware's configuration object.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
```javascript
|
|
13
|
-
var proxy = require("http-proxy-middleware");
|
|
14
|
-
var winston = require('winston');
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Context matching: decide which path(s) should be proxied. (wildcards supported)
|
|
18
|
-
**/
|
|
19
|
-
var context = '/api';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Proxy options
|
|
23
|
-
*/
|
|
24
|
-
var options = {
|
|
25
|
-
// hostname to the target server
|
|
26
|
-
target: 'http://localhost:3000',
|
|
27
|
-
|
|
28
|
-
// set correct host headers for name-based virtual hosted sites
|
|
29
|
-
changeOrigin: true,
|
|
30
|
-
|
|
31
|
-
// enable websocket proxying
|
|
32
|
-
ws: true,
|
|
33
|
-
|
|
34
|
-
// additional request headers
|
|
35
|
-
headers: {
|
|
36
|
-
'x-powered-by': 'foobar'
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
// rewrite paths
|
|
40
|
-
pathRewrite: {
|
|
41
|
-
'^/old/api': '/new/api', // rewrite path
|
|
42
|
-
'^/remove/api': '' // remove path
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
// re-target based on the request's host header and/or path
|
|
46
|
-
router: {
|
|
47
|
-
// host[/path] : <new target>
|
|
48
|
-
// /path : <new target>
|
|
49
|
-
'integration.localhost:8000' : 'http://localhost:8001', // host only
|
|
50
|
-
'staging.localhost:8000' : 'http://localhost:8002', // host only
|
|
51
|
-
'localhost:8000/api' : 'http://localhost:8003', // host + path
|
|
52
|
-
'/rest' : 'http://localhost:8004' // path only
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
// control logging
|
|
56
|
-
logLevel: 'silent',
|
|
57
|
-
|
|
58
|
-
// use a different lib for logging;
|
|
59
|
-
// i.e., write logs to file or server
|
|
60
|
-
logProvider: function (provider) {
|
|
61
|
-
return winston;
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
// subscribe to http-proxy's error event
|
|
65
|
-
onError: function onError(err, req, res) {
|
|
66
|
-
res.writeHead(500, {'Content-Type': 'text/plain'});
|
|
67
|
-
res.end('Something went wrong.');
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
// subscribe to http-proxy's proxyRes event
|
|
71
|
-
onProxyRes: function (proxyRes, req, res) {
|
|
72
|
-
proxyRes.headers['x-added'] = 'foobar';
|
|
73
|
-
delete proxyRes.headers['x-removed'];
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
// subscribe to http-proxy's proxyReq event
|
|
77
|
-
onProxyReq: function (proxyReq, req, res) {
|
|
78
|
-
// add custom header to request
|
|
79
|
-
proxyReq.setHeader('x-powered-by', 'foobar');
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* The following options are provided by Nodejitsu's http-proxy
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
// target
|
|
87
|
-
// forward
|
|
88
|
-
// agent
|
|
89
|
-
// ssl
|
|
90
|
-
// ws
|
|
91
|
-
// xfwd
|
|
92
|
-
// secure
|
|
93
|
-
// toProxy
|
|
94
|
-
// prependPath
|
|
95
|
-
// ignorePath
|
|
96
|
-
// localAddress
|
|
97
|
-
// changeOrigin
|
|
98
|
-
// auth
|
|
99
|
-
// hostRewrite
|
|
100
|
-
// autoRewrite
|
|
101
|
-
// protocolRewrite
|
|
102
|
-
// headers
|
|
103
|
-
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Create the proxy middleware, so it can be used in a server.
|
|
108
|
-
*/
|
|
109
|
-
var apiProxy = proxy(context, options);
|
|
110
|
-
```
|
package/recipes/basic.md
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
# Basic usage
|
|
2
|
-
|
|
3
|
-
This example will create a basic proxy middleware.
|
|
4
|
-
|
|
5
|
-
```javascript
|
|
6
|
-
var proxy = require("http-proxy-middleware");
|
|
7
|
-
|
|
8
|
-
var apiProxy = proxy('/api', {target: 'http://localhost:3000'});
|
|
9
|
-
// \____/ \________________________________/
|
|
10
|
-
// | |
|
|
11
|
-
// context options
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
## Alternative configuration
|
|
15
|
-
|
|
16
|
-
The proxy behavior of the following examples are **exactly** the same; Just different ways to configure it.
|
|
17
|
-
|
|
18
|
-
```javascript
|
|
19
|
-
app.use(proxy('/api', {target: 'http://localhost:3000', changeOrigin:true}));
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
```javascript
|
|
23
|
-
app.use(proxy('http://localhost:3000/api', {changeOrigin:true}));
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
```javascript
|
|
27
|
-
app.use('/api', proxy('http://localhost:3000', {changeOrigin:true}));
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
```javascript
|
|
31
|
-
app.use('/api', proxy({target: 'http://localhost:3000', changeOrigin:true}));
|
|
32
|
-
```
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
# Context matching
|
|
2
|
-
|
|
3
|
-
Determine which requests should be proxied.
|
|
4
|
-
|
|
5
|
-
Context matching is optional and is useful in cases where you are not able to use the regular [middleware mounting](http://expressjs.com/en/4x/api.html#app.use).
|
|
6
|
-
|
|
7
|
-
The [RFC 3986 `path`](https://tools.ietf.org/html/rfc3986#section-3.3) is used for context matching.
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
foo://example.com:8042/over/there?name=ferret#nose
|
|
11
|
-
\_/ \______________/\_________/ \_________/ \__/
|
|
12
|
-
| | | | |
|
|
13
|
-
scheme authority path query fragment
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
`http-proxy-middleware` offers several ways to do this:
|
|
18
|
-
|
|
19
|
-
<!-- MarkdownTOC autolink=true bracket=round -->
|
|
20
|
-
|
|
21
|
-
- [Path](#path)
|
|
22
|
-
- [Multi Path](#multi-path)
|
|
23
|
-
- [Wildcard](#wildcard)
|
|
24
|
-
- [Multi Wildcard](#multi-wildcard)
|
|
25
|
-
- [Wildcard / Exclusion](#wildcard--exclusion)
|
|
26
|
-
- [Custom filtering](#custom-filtering)
|
|
27
|
-
|
|
28
|
-
<!-- /MarkdownTOC -->
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
## Path
|
|
32
|
-
|
|
33
|
-
This will match paths starting with `/api`
|
|
34
|
-
|
|
35
|
-
```javascript
|
|
36
|
-
var proxy = require("http-proxy-middleware");
|
|
37
|
-
|
|
38
|
-
var apiProxy = proxy('/api', {target: 'http://localhost:3000'});
|
|
39
|
-
|
|
40
|
-
// `/api/foo/bar` -> `http://localhost:3000/api/foo/bar`
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
## Multi Path
|
|
44
|
-
|
|
45
|
-
This will match paths starting with `/api` or `/rest`
|
|
46
|
-
|
|
47
|
-
```javascript
|
|
48
|
-
var proxy = require("http-proxy-middleware");
|
|
49
|
-
|
|
50
|
-
var apiProxy = proxy(['/api', '/rest'], {target: 'http://localhost:3000'});
|
|
51
|
-
|
|
52
|
-
// `/api/foo/bar` -> `http://localhost:3000/api/foo/bar`
|
|
53
|
-
// `/rest/lorum/ipsum` -> `http://localhost:3000/rest/lorum/ipsum`
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Wildcard
|
|
57
|
-
|
|
58
|
-
This will match paths starting with `/api/` and should also end with `.json`
|
|
59
|
-
|
|
60
|
-
```javascript
|
|
61
|
-
var proxy = require("http-proxy-middleware");
|
|
62
|
-
|
|
63
|
-
var apiProxy = proxy('/api/**/*.json', {target: 'http://localhost:3000'});
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Multi Wildcard
|
|
67
|
-
|
|
68
|
-
Multiple wildcards can be used.
|
|
69
|
-
|
|
70
|
-
```javascript
|
|
71
|
-
var proxy = require("http-proxy-middleware");
|
|
72
|
-
|
|
73
|
-
var apiProxy = proxy(['/api/**/*.json', '/rest/**'], {target: 'http://localhost:3000'});
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## Wildcard / Exclusion
|
|
77
|
-
|
|
78
|
-
This example will create a proxy with wildcard context matching.
|
|
79
|
-
|
|
80
|
-
```javascript
|
|
81
|
-
var proxy = require("http-proxy-middleware");
|
|
82
|
-
|
|
83
|
-
var apiProxy = proxy(['foo/*.js', '!bar.js'], {target: 'http://localhost:3000'});
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
## Custom filtering
|
|
87
|
-
|
|
88
|
-
Write your custom context matching function to have full control on the matching behavior.
|
|
89
|
-
The request `pathname` and `req` object are provided to determine which requests should be proxied or not.
|
|
90
|
-
|
|
91
|
-
```javascript
|
|
92
|
-
var proxy = require("http-proxy-middleware");
|
|
93
|
-
|
|
94
|
-
var filter = function (pathname, req) {
|
|
95
|
-
return (pathname.match('^/api') && req.method === 'GET');
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
var apiProxy = proxy(filter, {target: 'http://localhost:3000'});
|
|
99
|
-
```
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# Corporate Proxy Support
|
|
2
|
-
|
|
3
|
-
This example will create a basic proxy middleware with corporate proxy support.
|
|
4
|
-
|
|
5
|
-
Provide a custom `http.agent` with [https-proxy-agent](https://github.com/TooTallNate/node-https-proxy-agent) to connect to the corporate proxy server.
|
|
6
|
-
|
|
7
|
-
```javascript
|
|
8
|
-
var HttpsProxyAgent = require('https-proxy-agent');
|
|
9
|
-
var proxy = require("http-proxy-middleware");
|
|
10
|
-
|
|
11
|
-
// corporate proxy to connect to
|
|
12
|
-
var proxyServer = process.env.HTTPS_PROXY ||
|
|
13
|
-
process.env.HTTP_PROXY;
|
|
14
|
-
|
|
15
|
-
var options = {
|
|
16
|
-
target: 'http://localhost:3000',
|
|
17
|
-
agent: new HttpsProxyAgent(proxyServer)
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
var apiProxy = proxy('/api', options);
|
|
21
|
-
```
|
package/recipes/delay.md
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
# Delay proxied request/response
|
|
2
|
-
|
|
3
|
-
Sometimes we need the ability to delay request to backend server or response from it back to client to test long execution time of particular url or all of them. With DevTool's [network throttling](https://developers.google.com/web/tools/chrome-devtools/profile/network-performance/network-conditions?hl=en) we can test slowdown of all request, not separately.
|
|
4
|
-
But we can handle each request individually via our proxy, and add delay to its execution time.
|
|
5
|
-
|
|
6
|
-
Let's assume that we want slow down the access to backend's `/api/get-me-something` resource. Delay request time by 2 seconds and increase response time by 5 seconds.
|
|
7
|
-
|
|
8
|
-
For achieving it just put additional route handler to your app before proxy handler:
|
|
9
|
-
|
|
10
|
-
```javascript
|
|
11
|
-
const myProxy = proxy({
|
|
12
|
-
target: 'http://www.example.com',
|
|
13
|
-
changeOrigin: true
|
|
14
|
-
});
|
|
15
|
-
const proxyDelay = function (req, res, next) {
|
|
16
|
-
if (req.originalUrl === '/api/get-me-something') {
|
|
17
|
-
// Delay request by 2 seconds
|
|
18
|
-
setTimeout(next, 2000);
|
|
19
|
-
|
|
20
|
-
// Delay response completion by 5 seconds
|
|
21
|
-
const endOriginal = res.end;
|
|
22
|
-
res.end = function (...args) {
|
|
23
|
-
setTimeout(function () {
|
|
24
|
-
endOriginal.apply(res, args);
|
|
25
|
-
}, 5000);
|
|
26
|
-
};
|
|
27
|
-
} else {
|
|
28
|
-
next();
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
app.use('/api', proxyDelay, myProxy);
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
And you will see result in devtools similar to this:
|
|
36
|
-
|
|
37
|
-

|
package/recipes/logLevel.md
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# Log Level
|
|
2
|
-
|
|
3
|
-
Control the amount of logging of http-proxy-middleware.
|
|
4
|
-
|
|
5
|
-
Possible values:
|
|
6
|
-
* `debug`
|
|
7
|
-
* `info`
|
|
8
|
-
* `warn` (default)
|
|
9
|
-
* `error`
|
|
10
|
-
* `silent`
|
|
11
|
-
|
|
12
|
-
## Level: debug
|
|
13
|
-
|
|
14
|
-
Log everyting.
|
|
15
|
-
|
|
16
|
-
```javascript
|
|
17
|
-
var proxy = require("http-proxy-middleware");
|
|
18
|
-
|
|
19
|
-
var options = {
|
|
20
|
-
target: 'http://localhost:3000',
|
|
21
|
-
logLevel: 'debug'
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
var apiProxy = proxy('/api', options);
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Level: silent
|
|
28
|
-
|
|
29
|
-
Suppress all logging.
|
|
30
|
-
|
|
31
|
-
```javascript
|
|
32
|
-
var proxy = require("http-proxy-middleware");
|
|
33
|
-
|
|
34
|
-
var options = {
|
|
35
|
-
target: 'http://localhost:3000',
|
|
36
|
-
logLevel: 'silent'
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
var apiProxy = proxy('/api', options);
|
|
40
|
-
```
|
package/recipes/logProvider.md
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
# Log Provider
|
|
2
|
-
|
|
3
|
-
Configure your own logger with the `logProvider` option.
|
|
4
|
-
|
|
5
|
-
In this example [winston](https://www.npmjs.com/package/winston) is configured to do the actual logging.
|
|
6
|
-
|
|
7
|
-
```javascript
|
|
8
|
-
var winston = require('winston');
|
|
9
|
-
var proxy = require("http-proxy-middleware");
|
|
10
|
-
|
|
11
|
-
var options = {
|
|
12
|
-
target: 'http://localhost:3000',
|
|
13
|
-
logProvider: function (provider) {
|
|
14
|
-
return winston;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
var apiProxy = proxy('/api', options);
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Winston
|
|
22
|
-
|
|
23
|
-
Configure your own logger with the `logProvider` option.
|
|
24
|
-
|
|
25
|
-
In this example [winston](https://www.npmjs.com/package/winston) is configured to do the actual logging. Map the logging api if needed.
|
|
26
|
-
|
|
27
|
-
```javascript
|
|
28
|
-
|
|
29
|
-
var winston = require('winston');
|
|
30
|
-
var proxy = require("http-proxy-middleware");
|
|
31
|
-
|
|
32
|
-
var logProvider = function (provider) {
|
|
33
|
-
return {
|
|
34
|
-
log : winston.log,
|
|
35
|
-
debug : winston.debug,
|
|
36
|
-
info : winston.info,
|
|
37
|
-
warn : winston.warn,
|
|
38
|
-
error : winston.error
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
var options = {
|
|
43
|
-
target: 'http://localhost:3000',
|
|
44
|
-
logProvider: logProvider
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
var apiProxy = proxy('/api', options);
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
# Winston Multi Transport
|
|
51
|
-
|
|
52
|
-
Configure your own logger with the `logProvider` option.
|
|
53
|
-
|
|
54
|
-
In this example [winston](https://www.npmjs.com/package/winston) is configured to do the actual logging.
|
|
55
|
-
|
|
56
|
-
```javascript
|
|
57
|
-
var winston = require('winston');
|
|
58
|
-
var proxy = require("http-proxy-middleware");
|
|
59
|
-
|
|
60
|
-
var logProvider = function (provider) {
|
|
61
|
-
var logger = new (winston.Logger)({
|
|
62
|
-
transports: [
|
|
63
|
-
new (winston.transports.Console)(),
|
|
64
|
-
new (winston.transports.File)({ filename: 'somefile.log' })
|
|
65
|
-
]
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
return logger;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
var options = {
|
|
72
|
-
target: 'http://localhost:3000',
|
|
73
|
-
logProvider: logProvider
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
var apiProxy = proxy('/api', options);
|
|
77
|
-
```
|
package/recipes/modify-post.md
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
##Modify Post Parameters:
|
|
2
|
-
The code example below illustrates how to modify POST body data prior to forwarding to the proxy target.
|
|
3
|
-
Key to this example is the *"OnProxyReq"* event handler that creates a new POST body that can be manipulated to format the POST data as required. For example: inject new POST parameters that should only be visible server side.
|
|
4
|
-
|
|
5
|
-
This example uses the *"body-parser"* module in the main app to create a req.body object with the decoded POST parameters. Side note - the code below will allow *"http-proxy-middleware"* to work with *"body-parser"*.
|
|
6
|
-
|
|
7
|
-
Since this only modifys the request body stream the original POST body parameters remain in tact, so any POST data changes will not be sent back in the response to the client.
|
|
8
|
-
|
|
9
|
-
## Example:
|
|
10
|
-
|
|
11
|
-
'use strict';
|
|
12
|
-
|
|
13
|
-
var express = require('express');
|
|
14
|
-
var ProxyMiddleware = require('http-proxy-middleware');
|
|
15
|
-
var router = express.Router();
|
|
16
|
-
|
|
17
|
-
var proxy_filter = function (path, req) {
|
|
18
|
-
return path.match('^/docs') && ( req.method === 'GET' || req.method === 'POST' );
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
var proxy_options = {
|
|
22
|
-
target: 'http://localhost:8080',
|
|
23
|
-
pathRewrite: {
|
|
24
|
-
'^/docs' : '/java/rep/server1' // Host path & target path conversion
|
|
25
|
-
},
|
|
26
|
-
onError(err, req, res) {
|
|
27
|
-
res.writeHead(500, {
|
|
28
|
-
'Content-Type': 'text/plain'
|
|
29
|
-
});
|
|
30
|
-
res.end('Something went wrong. And we are reporting a custom error message.' + err);
|
|
31
|
-
},
|
|
32
|
-
onProxyReq(proxyReq, req, res) {
|
|
33
|
-
if ( req.method == "POST" && req.body ) {
|
|
34
|
-
// Add req.body logic here if needed....
|
|
35
|
-
|
|
36
|
-
// ....
|
|
37
|
-
|
|
38
|
-
// Remove body-parser body object from the request
|
|
39
|
-
if ( req.body ) delete req.body;
|
|
40
|
-
|
|
41
|
-
// Make any needed POST parameter changes
|
|
42
|
-
let body = new Object();
|
|
43
|
-
|
|
44
|
-
body.filename = 'reports/statistics/summary_2016.pdf';
|
|
45
|
-
body.routeid = 's003b012d002';
|
|
46
|
-
body.authid = 'bac02c1d-258a-4177-9da6-862580154960';
|
|
47
|
-
|
|
48
|
-
// URI encode JSON object
|
|
49
|
-
body = Object.keys( body ).map(function( key ) {
|
|
50
|
-
return encodeURIComponent( key ) + '=' + encodeURIComponent( body[ key ])
|
|
51
|
-
}).join('&');
|
|
52
|
-
|
|
53
|
-
// Update header
|
|
54
|
-
proxyReq.setHeader( 'content-type', 'application/x-www-form-urlencoded' );
|
|
55
|
-
proxyReq.setHeader( 'content-length', body.length );
|
|
56
|
-
|
|
57
|
-
// Write out body changes to the proxyReq stream
|
|
58
|
-
proxyReq.write( body );
|
|
59
|
-
proxyReq.end();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// Proxy configuration
|
|
65
|
-
var proxy = ProxyMiddleware( proxy_filter, proxy_options );
|
|
66
|
-
|
|
67
|
-
/* GET home page. */
|
|
68
|
-
router.get('/', function(req, res, next) {
|
|
69
|
-
res.render('index', { title: 'Node.js Express Proxy Test' });
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
router.all('/docs', proxy );
|
|
73
|
-
|
|
74
|
-
module.exports = router;
|
package/recipes/pathRewrite.md
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
# pathRewrite
|
|
2
|
-
|
|
3
|
-
Modify request paths before requests are send to the target.
|
|
4
|
-
|
|
5
|
-
<!-- MarkdownTOC autolink=true bracket=round -->
|
|
6
|
-
|
|
7
|
-
- [rewrite paths](#rewrite-paths)
|
|
8
|
-
- [remove paths](#remove-paths)
|
|
9
|
-
- [add paths](#add-paths)
|
|
10
|
-
- [custom rewrite function](#custom-rewrite-function)
|
|
11
|
-
|
|
12
|
-
<!-- /MarkdownTOC -->
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
## rewrite paths
|
|
16
|
-
|
|
17
|
-
Rewrite paths
|
|
18
|
-
|
|
19
|
-
```javascript
|
|
20
|
-
var proxy = require("http-proxy-middleware");
|
|
21
|
-
|
|
22
|
-
var options = {
|
|
23
|
-
target: 'http://localhost:3000',
|
|
24
|
-
pathRewrite: {
|
|
25
|
-
"^/old/api" : "/new/api" // rewrite path
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
var apiProxy = proxy('/api', options);
|
|
30
|
-
|
|
31
|
-
// `/old/api/foo/bar` -> `http://localhost:3000/new/api/foo/bar`
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
## remove paths
|
|
35
|
-
|
|
36
|
-
Remove base path
|
|
37
|
-
|
|
38
|
-
```javascript
|
|
39
|
-
var proxy = require("http-proxy-middleware");
|
|
40
|
-
|
|
41
|
-
var options = {
|
|
42
|
-
target: 'http://localhost:3000',
|
|
43
|
-
pathRewrite: {
|
|
44
|
-
"^/remove/api" : "" // remove base path
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
var apiProxy = proxy('/api', options);
|
|
49
|
-
|
|
50
|
-
// `/remove/api/lorum/ipsum` -> `http://localhost:3000/lorum/ipsum`
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
## add paths
|
|
54
|
-
|
|
55
|
-
Add base path
|
|
56
|
-
|
|
57
|
-
```javascript
|
|
58
|
-
var proxy = require("http-proxy-middleware");
|
|
59
|
-
|
|
60
|
-
var options = {
|
|
61
|
-
target: 'http://localhost:3000',
|
|
62
|
-
pathRewrite: {
|
|
63
|
-
"^/" : "/extra/" // add base path
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
var apiProxy = proxy('/api', options);
|
|
68
|
-
|
|
69
|
-
// `/api/lorum/ipsum` -> `http://localhost:3000/extra/api/lorum/ipsum`
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
## custom rewrite function
|
|
73
|
-
|
|
74
|
-
Implement you own path rewrite logic.
|
|
75
|
-
|
|
76
|
-
The unmodified path will be used, when rewrite function returns `undefined`
|
|
77
|
-
|
|
78
|
-
```javascript
|
|
79
|
-
var proxy = require("http-proxy-middleware");
|
|
80
|
-
|
|
81
|
-
var rewriteFn = function (path, req) {
|
|
82
|
-
return path.replace('/api/foo', '/api/bar');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
var options = {
|
|
86
|
-
target: 'http://localhost:3000',
|
|
87
|
-
pathRewrite: rewriteFn
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
var apiProxy = proxy('/api', options);
|
|
91
|
-
|
|
92
|
-
// `/api/foo/lorum/ipsum` -> `http://localhost:3000/api/bar/lorum/ipsum`
|
|
93
|
-
```
|