-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
51 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,3 @@ | ||
# Recipes | ||
|
||
Common usages of `http-proxy-middleware`. | ||
|
||
# Configuration example | ||
|
||
Overview of `http-proxy-middleware` specific options. | ||
|
||
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. | ||
|
||
```javascript | ||
const { createProxyMiddleware } = require('http-proxy-middleware'); | ||
const winston = require('winston'); | ||
|
||
/** | ||
* Proxy options | ||
*/ | ||
const options = { | ||
// decide which path(s) should be proxied. (wildcards supported) | ||
pathFilter: '/api', | ||
|
||
// hostname to the target server | ||
target: 'http://localhost:3000', | ||
|
||
// set correct host headers for name-based virtual hosted sites | ||
changeOrigin: true, | ||
|
||
// enable websocket proxying | ||
ws: true, | ||
|
||
// additional request headers | ||
headers: { | ||
'x-powered-by': 'foobar', | ||
}, | ||
|
||
// rewrite paths | ||
pathRewrite: { | ||
'^/api/old-path': '/api/new-path', // rewrite path | ||
'^/api/remove/path': '/path', // remove base path | ||
}, | ||
|
||
// re-target based on the request's host header and/or path | ||
router: { | ||
// host[/path] : <new target> | ||
// /path : <new target> | ||
'integration.localhost:8000': 'http://localhost:8001', // host only | ||
'staging.localhost:8000': 'http://localhost:8002', // host only | ||
'localhost:8000/api': 'http://localhost:8003', // host + path | ||
'/rest': 'http://localhost:8004', // path only | ||
}, | ||
|
||
// control logging | ||
logLevel: 'silent', | ||
|
||
// use a different lib for logging; | ||
// i.e., write logs to file or server | ||
logProvider: function (provider) { | ||
return winston; | ||
}, | ||
|
||
// subscribe to http-proxy's error event | ||
onError: function onError(err, req, res) { | ||
res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
res.end('Something went wrong.'); | ||
}, | ||
|
||
// subscribe to http-proxy's proxyRes event | ||
onProxyRes: function (proxyRes, req, res) { | ||
proxyRes.headers['x-added'] = 'foobar'; | ||
delete proxyRes.headers['x-removed']; | ||
}, | ||
|
||
// subscribe to http-proxy's proxyReq event | ||
onProxyReq: function (proxyReq, req, res) { | ||
// add custom header to request | ||
proxyReq.setHeader('x-powered-by', 'foobar'); | ||
}, | ||
|
||
/** | ||
* The following options are provided by Nodejitsu's http-proxy | ||
*/ | ||
|
||
// target | ||
// forward | ||
// agent | ||
// ssl | ||
// ws | ||
// xfwd | ||
// secure | ||
// toProxy | ||
// prependPath | ||
// ignorePath | ||
// localAddress | ||
// changeOrigin | ||
// auth | ||
// hostRewrite | ||
// autoRewrite | ||
// protocolRewrite | ||
// headers | ||
}; | ||
|
||
/** | ||
* Create the proxy middleware, so it can be used in a server. | ||
*/ | ||
const apiProxy = createProxyMiddleware(options); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# [BREAKING CHANGE] | ||
|
||
This functionality is removed in v3. | ||
|
||
The old "context matching" function has been moved to the [pathFilter](pathFilter.md) configuration property. | ||
|
||
TL;DR | ||
|
||
```js | ||
createProxyMiddleware('/api', { | ||
target: 'http://localhost:3000', | ||
}); | ||
|
||
createProxyMiddleware({ | ||
target: 'http://localhost:3000', | ||
pathFilter: '/api', | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.