http-proxy-middleware
Advanced tools
Comparing version 0.13.0 to 0.14.0
# Changelog | ||
## [v0.14.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.14.0) | ||
- feat(proxy): support proxy creation without context. | ||
- fix(connect mounting): use connect's `path` configuration to mount proxy. | ||
## [v0.13.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.13.0) | ||
@@ -4,0 +8,0 @@ - feat(context): custom context matcher; when simple `path` matching is not sufficient. |
/** | ||
* Module dependencies. | ||
*/ | ||
var browserSync = require('../../node_modules/browser-sync/index').create(); // require('browser-sync').create(); | ||
var proxyMiddleware = require('../../index'); // require('http-proxy-middleware'); | ||
var browserSync = require('browser-sync').create(); | ||
var proxy = require('../../index'); // require('http-proxy-middleware'); | ||
// configure proxy middleware | ||
// context: '/' will proxy all requests | ||
// use: '/api' to proxy request when path starts with '/api' | ||
var proxy = proxyMiddleware('/api', { | ||
target: 'http://www.example.org', | ||
changeOrigin: true // for vhosted sites, changes host header to match to target's host | ||
}); | ||
/** | ||
* Configure proxy middleware | ||
*/ | ||
var chuckNorrisApiProxy = proxy('/jokes', { | ||
target: 'http://api.icndb.com', | ||
changeOrigin: true, // for vhosted sites, changes host header to match to target's host | ||
logLevel: 'debug' | ||
}); | ||
/** | ||
* Add the proxy to browser-sync | ||
*/ | ||
browserSync.init({ | ||
@@ -19,9 +23,8 @@ server: { | ||
port: 3000, | ||
middleware: [proxy], // add the proxy to browser-sync | ||
middleware: [chuckNorrisApiProxy], | ||
}, | ||
startPath: '/api' | ||
startPath: '/jokes/random/5?limitTo=[nerdy]' | ||
}); | ||
console.log('listening on port 3000'); | ||
console.log('try:'); | ||
console.log(' http://localhost:3000/api'); | ||
console.log('[DEMO] Server: listening on port 3000'); | ||
console.log('[DEMO] Opening: http://localhost:3000/jokes/random/5?limitTo=[nerdy]'); |
/** | ||
* Module dependencies. | ||
*/ | ||
var http = require('http'); // require('http'); | ||
var connect = require('../../node_modules/connect/index'); // require('connect'); | ||
var proxyMiddleware = require('../../index'); // require('http-proxy-middleware'); | ||
var http = require('http'); | ||
var connect = require('connect'); | ||
var proxy = require('../../index'); // require('http-proxy-middleware'); | ||
// configure proxy middleware | ||
// context: '/' will proxy all requests | ||
// use: '/api' to proxy request when path starts with '/api' | ||
var proxy = proxyMiddleware('/api', { | ||
target: 'http://www.example.org', | ||
changeOrigin: true // for vhosted sites, changes host header to match to target's host | ||
}); | ||
/** | ||
* Configure proxy middleware | ||
*/ | ||
var chuckNorrisApiProxy = proxy('/jokes', { | ||
target: 'http://api.icndb.com', | ||
changeOrigin: true, // for vhosted sites, changes host header to match to target's host | ||
logLevel: 'debug' | ||
}); | ||
var app = connect(); | ||
app.use(proxy); // add the proxy to connect | ||
/** | ||
* Add the proxy to connect | ||
*/ | ||
app.use(chuckNorrisApiProxy); | ||
http.createServer(app).listen(3000); | ||
console.log('listening on port 3000'); | ||
console.log('try:'); | ||
console.log(' http://localhost:3000/api'); | ||
console.log('[DEMO] Server: listening on port 3000'); | ||
console.log('[DEMO] Opening: http://localhost:3000/api'); | ||
require('open')('http://localhost:3000/jokes/random/5?limitTo=[nerdy]'); |
/** | ||
* Module dependencies. | ||
*/ | ||
var express = require('../../node_modules/express/index'); // require('express'); | ||
var proxyMiddleware = require('../../index'); // require('http-proxy-middleware'); | ||
var express = require('express'); | ||
var proxy = require('../../index'); // require('http-proxy-middleware'); | ||
// configure proxy middleware | ||
// context: '/' will proxy all requests | ||
// use: '/api' to proxy request when path starts with '/api' | ||
var proxy = proxyMiddleware('/api', { | ||
target: 'http://www.example.org', | ||
changeOrigin: true // for vhosted sites, changes host header to match to target's host | ||
}); | ||
/** | ||
* Configure proxy middleware | ||
*/ | ||
var chuckNorrisApiProxy = proxy('/jokes', { | ||
target: 'http://api.icndb.com', | ||
changeOrigin: true, // for vhosted sites, changes host header to match to target's host | ||
logLevel: 'debug' | ||
}); | ||
var app = express(); | ||
app.use(proxy); // add the proxy to express | ||
/** | ||
* Add the proxy to express | ||
*/ | ||
app.use(chuckNorrisApiProxy); | ||
app.listen(3000); | ||
console.log('listening on port 3000'); | ||
console.log('try:'); | ||
console.log(' http://localhost:3000/api'); | ||
console.log('[DEMO] Server: listening on port 3000'); | ||
console.log('[DEMO] Opening: http://localhost:3000/api'); | ||
require('open')('http://localhost:3000/jokes/random/5?limitTo=[nerdy]'); |
/** | ||
* Module dependencies. | ||
*/ | ||
var express = require('../../node_modules/express/index'); // require('express'); | ||
var proxyMiddleware = require('../../index'); // require('http-proxy-middleware'); | ||
var express = require('express'); | ||
var proxy = require('../../index'); // require('http-proxy-middleware'); | ||
// configure proxy middleware | ||
// context: '/' will proxy all requests | ||
var proxy = proxyMiddleware('/', { | ||
/** | ||
* Configure proxy middleware | ||
*/ | ||
var wsProxy = proxy('/', { | ||
target: 'http://echo.websocket.org', | ||
@@ -16,4 +17,4 @@ // pathRewrite: { | ||
changeOrigin: true, // for vhosted sites, changes host header to match to target's host | ||
ws: true // enable websocket proxy | ||
ws: true, // enable websocket proxy | ||
logLevel: 'debug' | ||
}); | ||
@@ -23,12 +24,13 @@ | ||
app.use('/', express.static(__dirname)); // demo page | ||
app.use(proxy); // add the proxy to express | ||
app.use(wsProxy); // add the proxy to express | ||
var server = app.listen(3000); | ||
server.on('upgrade', proxy.upgrade); // optional: upgrade externally | ||
server.on('upgrade', wsProxy.upgrade); // optional: upgrade externally | ||
console.log('listening on port 3000'); | ||
console.log('try:'); | ||
console.log(' http://localhost:3000 for a demo'); | ||
console.log(' ws://localhost:3000 requests will be proxied to ws://echo.websocket.org'); | ||
console.log('[DEMO] Server: listening on port 3000'); | ||
console.log('[DEMO] Opening: http://localhost:3000'); | ||
require('open')('http://localhost:3000'); | ||
/** | ||
@@ -35,0 +37,0 @@ * Example: |
@@ -38,5 +38,3 @@ var httpProxy = require('http-proxy'); | ||
// https://github.com/chimurai/http-proxy-middleware/issues/17 | ||
if (req.baseUrl) { | ||
req.url = req.originalUrl; | ||
} | ||
req.url = req.originalUrl; | ||
@@ -43,0 +41,0 @@ if (contextMatcher.match(config.context, req.url, req)) { |
@@ -16,5 +16,10 @@ var _ = require('lodash'); | ||
var useShortHand = isShortHand(context); | ||
if (useShortHand) { | ||
// app.use('/api', proxy({target:'http://localhost:9000'})); | ||
if (isContexless(context, opts)) { | ||
config.context = '/'; | ||
config.options = _.assign(config.options, context); | ||
} | ||
// app.use('/api', proxy('http://localhost:9000')); | ||
// app.use(proxy('http://localhost:9000/api')); | ||
else if (isStringShortHand(context)) { | ||
var oUrl = url.parse(context); | ||
@@ -29,3 +34,3 @@ var target = [oUrl.protocol, '//', oUrl.host].join(''); | ||
} | ||
// app.use('/api', proxy({target:'http://localhost:9000'})); | ||
} else { | ||
@@ -46,5 +51,16 @@ config.context = context; | ||
return config; | ||
}; | ||
} | ||
function isShortHand(context) { | ||
/** | ||
* Checks if a String only target/config is provided. | ||
* This can be just the host or with the optional path. | ||
* | ||
* @example | ||
* app.use('/api', proxy('http://localhost:9000')); | ||
app.use(proxy('http://localhost:9000/api')); | ||
* | ||
* @param {String} context [description] | ||
* @return {Boolean} [description] | ||
*/ | ||
function isStringShortHand(context) { | ||
if (_.isString(context)) { | ||
@@ -55,2 +71,17 @@ return (url.parse(context).host) ? true : false; | ||
/** | ||
* Checks if a Object only config is provided, without a context. | ||
* In this case the all paths will be proxied. | ||
* | ||
* @example | ||
* app.use('/api', proxy({target:'http://localhost:9000'})); | ||
* | ||
* @param {Object} context [description] | ||
* @param {*} opts [description] | ||
* @return {Boolean} [description] | ||
*/ | ||
function isContexless(context, opts) { | ||
return (_.isPlainObject(context) && _.isEmpty(opts)); | ||
} | ||
function mapLegacyProxyHostOption(options) { | ||
@@ -57,0 +88,0 @@ // set options.headers.host when option.proxyHost is provided |
{ | ||
"name": "http-proxy-middleware", | ||
"version": "0.13.0", | ||
"version": "0.14.0", | ||
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync", | ||
@@ -46,2 +46,3 @@ "main": "index.js", | ||
"mocha-lcov-reporter": "1.2.0", | ||
"open": "0.0.5", | ||
"ws": "^1.0.1" | ||
@@ -48,0 +49,0 @@ }, |
166
README.md
@@ -6,8 +6,28 @@ # http-proxy-middleware | ||
[![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) | ||
[![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) | ||
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). | ||
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). | ||
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) | ||
## Table of Contents | ||
<!-- MarkdownTOC autolink=true bracket=round depth=2 --> | ||
- [Install](#install) | ||
- [Core concept](#core-concept) | ||
- [Example](#example) | ||
- [Context matching](#context-matching) | ||
- [Shorthand](#shorthand) | ||
- [WebSocket](#websocket) | ||
- [Options](#options) | ||
- [Working examples](#working-examples) | ||
- [Recipes](#recipes) | ||
- [Compatible servers](#compatible-servers) | ||
- [Tests](#tests) | ||
- [Changelog](#changelog) | ||
- [License](#license) | ||
<!-- /MarkdownTOC --> | ||
## Install | ||
@@ -22,11 +42,12 @@ | ||
Configure the proxy middleware. | ||
```javascript | ||
var proxyMiddleware = require('http-proxy-middleware'); | ||
var proxy = require('http-proxy-middleware'); | ||
var proxy = proxyMiddleware('/api', {target: 'http://www.example.org'}); | ||
// \____/ \________________________________/ | ||
// | | | ||
// context options | ||
var apiProxy = proxy('/api', {target: 'http://www.example.org'}); | ||
// \____/ \_____________________________/ | ||
// | | | ||
// context options | ||
// 'proxy' is now ready to be used in a server. | ||
// 'apiProxy' is now ready to be used as middleware in a server. | ||
``` | ||
@@ -36,8 +57,7 @@ * **context**: matches provided context against request-urls' **path**. | ||
Example: `'/api'` or `['/api', '/ajax']`. (more about [context matching](#context-matching)) | ||
* **options.target**: target host to proxy to. | ||
Check out available [proxy middleware options](#options). | ||
* **options.target**: target host to proxy to. (full list of [proxy middleware options](#options)) | ||
``` javascript | ||
// shorthand syntax for the example above: | ||
var proxy = proxyMiddleware('http://www.example.org/api'); | ||
var apiProxy = proxy('http://www.example.org/api'); | ||
@@ -49,10 +69,12 @@ ``` | ||
An example with express server. | ||
An example with `express` server. | ||
```javascript | ||
// include dependencies | ||
var express = require('express'); | ||
var proxyMiddleware = require('http-proxy-middleware'); | ||
var proxy = require('http-proxy-middleware'); | ||
// configure proxy middleware context | ||
var context = '/api'; // requests with this path will be proxied | ||
// use Array for multipath: ['/api', '/rest'] | ||
@@ -76,12 +98,10 @@ // configure proxy middleware options | ||
// create the proxy | ||
var proxy = proxyMiddleware(context, options); | ||
var apiProxy = proxy(context, options); | ||
// use the configured `proxy` in web server | ||
// use the configured `apiProxy` in web server | ||
var app = express(); | ||
app.use(proxy); | ||
app.use(apiProxy); | ||
app.listen(3000); | ||
``` | ||
Check out [working examples](#more-examples). | ||
**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`. | ||
@@ -91,3 +111,3 @@ | ||
http-proxy-middleware offers several ways to decide which requests should be proxied. | ||
`http-proxy-middleware` offers several ways to decide which requests should be proxied. | ||
Request URL's [ _path-absolute_ and _query_](https://tools.ietf.org/html/rfc3986#section-3) will be used for context matching . | ||
@@ -120,3 +140,3 @@ | ||
var apiProxy = proxyMiddleware(filter, {target: 'http://www.example.org'}) | ||
var apiProxy = proxy(filter, {target: 'http://www.example.org'}) | ||
``` | ||
@@ -129,14 +149,27 @@ | ||
```javascript | ||
proxyMiddleware('http://www.example.org:8000/api'); | ||
// proxyMiddleware('/api', {target: 'http://www.example.org:8000'}); | ||
proxy('http://www.example.org:8000/api'); | ||
// proxy('/api', {target: 'http://www.example.org:8000'}); | ||
proxyMiddleware('http://www.example.org:8000/api/books/*/**.json'); | ||
// proxyMiddleware('/api/books/*/**.json', {target: 'http://www.example.org:8000'}); | ||
proxy('http://www.example.org:8000/api/books/*/**.json'); | ||
// proxy('/api/books/*/**.json', {target: 'http://www.example.org:8000'}); | ||
proxyMiddleware('http://www.example.org:8000/api', {changeOrigin:true}); | ||
// proxyMiddleware('/api', {target: 'http://www.example.org:8000', changeOrigin: true}); | ||
proxy('http://www.example.org:8000/api', {changeOrigin:true}); | ||
// proxy('/api', {target: 'http://www.example.org:8000', changeOrigin: true}); | ||
``` | ||
### app.use(path, proxy) | ||
If you want to use the server's `app.use` `path` parameter to match requests; | ||
Create and mount the proxy without the http-proxy-middleware `context` parameter: | ||
```javascript | ||
app.use('/api', proxy({target:'http://www.example.org', changeOrigin:true})); | ||
``` | ||
`app.use` documentation: | ||
* express: http://expressjs.com/en/4x/api.html#app.use | ||
* connect: https://github.com/senchalabs/connect#mount-middleware | ||
## WebSocket | ||
@@ -146,9 +179,9 @@ | ||
// verbose api | ||
proxyMiddleware('/', {target:'http://echo.websocket.org', ws:true}); | ||
proxy('/', {target:'http://echo.websocket.org', ws:true}); | ||
// shorthand | ||
proxyMiddleware('http://echo.websocket.org', {ws:true}); | ||
proxy('http://echo.websocket.org', {ws:true}); | ||
// shorter shorthand | ||
proxyMiddleware('ws://echo.websocket.org'); | ||
proxy('ws://echo.websocket.org'); | ||
``` | ||
@@ -160,9 +193,9 @@ | ||
```javascript | ||
var proxy = proxyMiddleware('ws://echo.websocket.org', {changeOrigin:true}); | ||
var wsProxy = proxy('ws://echo.websocket.org', {changeOrigin:true}); | ||
var app = express(); | ||
app.use(proxy); | ||
app.use(wsProxy); | ||
var server = app.listen(3000); | ||
server.on('upgrade', proxy.upgrade); // <-- subscribe to http 'upgrade' | ||
server.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade' | ||
``` | ||
@@ -187,10 +220,10 @@ | ||
proxyTable: { | ||
"integration.localhost:3000" : "http://localhost:8001", // host only | ||
"staging.localhost:3000" : "http://localhost:8002", // host only | ||
"localhost:3000/api" : "http://localhost:8003", // host + path | ||
"/rest" : "http://localhost:8004" // path only | ||
"integration.localhost:3000" : "http://localhost:8001", // host only | ||
"staging.localhost:3000" : "http://localhost:8002", // host only | ||
"localhost:3000/api" : "http://localhost:8003", // host + path | ||
"/rest" : "http://localhost:8004" // path only | ||
} | ||
``` | ||
* **option.logLevel**: string, ['debug', 'info', 'warn', 'error', 'silent']. Default: 'info' | ||
* **option.logLevel**: string, ['debug', 'info', 'warn', 'error', 'silent']. Default: `'info'` | ||
@@ -222,2 +255,5 @@ * **option.logProvider**: function, modify or replace log provider. Default: `console`. | ||
### Events | ||
Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events): | ||
* **option.onError**: function, subscribe to http-proxy's `error` event for custom error handling. | ||
@@ -276,4 +312,6 @@ ```javascript | ||
The following options are provided by the underlying [http-proxy](https://www.npmjs.com/package/http-proxy). | ||
### http-proxy options | ||
The following options are provided by the underlying [http-proxy](https://github.com/nodejitsu/node-http-proxy#options). | ||
* **option.target**: url string to be parsed with the url module | ||
@@ -286,3 +324,3 @@ * **option.forward**: url string to be parsed with the url module | ||
* **option.secure**: true/false, if you want to verify the SSL Certs | ||
* **option.toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies) | ||
* **option.toProxy**: true/false, passes the absolute URL as the `path` (useful for proxying to proxies) | ||
* **option.prependPath**: true/false, Default: true - specify whether you want to prepend the target's path to the proxy path> | ||
@@ -298,38 +336,32 @@ * **option.ignorePath**: true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request> | ||
## Recipes | ||
## Working examples | ||
View the [recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes) for common use cases. | ||
View and play around with [working examples](https://github.com/chimurai/http-proxy-middleware/tree/master/examples). | ||
## More Examples | ||
* Browser-Sync ([exampe source](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js)) | ||
* express ([exampe source](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js)) | ||
* connect ([exampe source](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js)) | ||
* WebSocket ([exampe source](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js)) | ||
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: | ||
## Recipes | ||
```bash | ||
$ git clone https://github.com/chimurai/http-proxy-middleware.git | ||
$ cd http-proxy-middleware | ||
$ npm install | ||
``` | ||
View the [recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes) for common use cases. | ||
Run the example: | ||
```bash | ||
$ node examples/connect | ||
``` | ||
Or just explore the proxy examples' sources: | ||
* `examples/connect` - [connect proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js) | ||
* `examples/express` - [express proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js) | ||
* `examples/browser-sync` - [browser-sync proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js) | ||
* `examples/websocket` - [websocket proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js) with express | ||
## Compatible servers | ||
http-proxy-middleware is compatible with the following servers: | ||
`http-proxy-middleware` is compatible with the following servers: | ||
* [connect](https://www.npmjs.com/package/connect) | ||
* [express](https://www.npmjs.com/package/express) | ||
* [browser-sync](https://www.npmjs.com/package/browser-sync) | ||
* [lite-server](https://www.npmjs.com/package/lite-server) | ||
* [grunt-contrib-connect](https://www.npmjs.com/package/grunt-contrib-connect) | ||
* [grunt-browser-sync](https://www.npmjs.com/package/grunt-browser-sync) | ||
* [gulp-connect](https://www.npmjs.com/package/gulp-connect) | ||
* [gulp-webserver](https://www.npmjs.com/package/gulp-webserver) | ||
Sample implementations can be found in the [server recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes/servers.md). | ||
## Tests | ||
To run the test suite, first install the dependencies, then run: | ||
Run the test suite: | ||
@@ -339,6 +371,14 @@ ```bash | ||
$ npm install | ||
``` | ||
unit testing | ||
```bash | ||
# unit tests | ||
$ npm test | ||
``` | ||
coverage | ||
```bash | ||
# code coverage | ||
@@ -357,2 +397,2 @@ $ npm run cover | ||
Copyright (c) 2015 Steven Chim | ||
Copyright (c) 2015-2016 Steven Chim |
@@ -6,5 +6,5 @@ # Basic usage | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
var proxy = proxyMiddleware('/api', {target: 'http://localhost:3000'}); | ||
var apiProxy = proxy('/api', {target: 'http://localhost:3000'}); | ||
// \____/ \________________________________/ | ||
@@ -14,1 +14,21 @@ // | | | ||
``` | ||
## Alternative configuration | ||
The proxy behavior of the following examples are **exactly** the same; Just different ways to configure it. | ||
```javascript | ||
app.use(proxy('/api', {target: 'http://localhost:3000', changeOrigin:true})); | ||
``` | ||
```javascript | ||
app.use(proxy('http://localhost:3000/api', {changeOrigin:true})); | ||
``` | ||
```javascript | ||
app.use('/api', proxy('http://localhost:3000', {changeOrigin:true})); | ||
``` | ||
```javascript | ||
app.use('/api', proxy({target: 'http://localhost:3000', changeOrigin:true})); | ||
``` |
@@ -9,3 +9,3 @@ # Corporate Proxy Support | ||
var HttpsProxyAgent = require('https-proxy-agent'); | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
@@ -21,3 +21,3 @@ // corporate proxy to connect to | ||
var proxy = proxyMiddleware('/api', options); | ||
var apiProxy = proxy('/api', options); | ||
``` |
@@ -17,3 +17,3 @@ # Log Level | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
@@ -25,3 +25,3 @@ var options = { | ||
var proxy = proxyMiddleware('/api', options); | ||
var apiProxy = proxy('/api', options); | ||
``` | ||
@@ -34,3 +34,3 @@ | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
@@ -42,3 +42,3 @@ var options = { | ||
var proxy = proxyMiddleware('/api', options); | ||
var apiProxy = proxy('/api', options); | ||
``` |
@@ -9,3 +9,3 @@ # Log Provider | ||
var winston = require('winston'); | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
@@ -19,3 +19,61 @@ var options = { | ||
var proxy = proxyMiddleware('/api', options); | ||
var apiProxy = proxy('/api', options); | ||
``` | ||
## Winston | ||
Configure your own logger with the `logProvider` option. | ||
In this example [winston](https://www.npmjs.com/package/winston) is configured to do the actual logging. Map the logging api if needed. | ||
```javascript | ||
var winston = require('winston'); | ||
var proxy = require("http-proxy-middleware"); | ||
var logProvider = function (provider) { | ||
return { | ||
log : winston.log, | ||
debug : winston.debug, | ||
info : winston.info, | ||
warn : winston.warn, | ||
error : winston.error | ||
}; | ||
}; | ||
var options = { | ||
target: 'http://localhost:3000', | ||
logProvider: logProvider | ||
}; | ||
var apiProxy = proxy('/api', options); | ||
``` | ||
# Winston Multi Transport | ||
Configure your own logger with the `logProvider` option. | ||
In this example [winston](https://www.npmjs.com/package/winston) is configured to do the actual logging. | ||
```javascript | ||
var winston = require('winston'); | ||
var proxy = require("http-proxy-middleware"); | ||
var logProvider = function (provider) { | ||
var logger = new (winston.Logger)({ | ||
transports: [ | ||
new (winston.transports.Console)(), | ||
new (winston.transports.File)({ filename: 'somefile.log' }) | ||
] | ||
}); | ||
return logger; | ||
}; | ||
var options = { | ||
target: 'http://localhost:3000', | ||
logProvider: logProvider | ||
}; | ||
var apiProxy = proxy('/api', options); | ||
``` |
# pathRewrite | ||
Rewrite paths before requests are send to the target. | ||
Modify request paths before requests are send to the target. | ||
## Path rewrite | ||
<!-- MarkdownTOC autolink=true bracket=round --> | ||
- [rewrite paths](#rewrite-paths) | ||
- [remove paths](#remove-paths) | ||
- [add paths](#add-paths) | ||
<!-- /MarkdownTOC --> | ||
## rewrite paths | ||
Rewrite paths | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
@@ -16,3 +28,3 @@ var options = { | ||
var proxy = proxyMiddleware('/api', options); | ||
var apiProxy = proxy('/api', options); | ||
@@ -22,5 +34,8 @@ // `/old/api/foo/bar` -> `http://localhost:3000/new/api/foo/bar` | ||
## Remove base path | ||
## remove paths | ||
Remove base path | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
@@ -34,3 +49,3 @@ var options = { | ||
var proxy = proxyMiddleware('/api', options); | ||
var apiProxy = proxy('/api', options); | ||
@@ -40,5 +55,8 @@ // `/remove/api/lorum/ipsum` -> `http://localhost:3000/lorum/ipsum` | ||
## Add base path | ||
## add paths | ||
Add base path | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
@@ -52,5 +70,5 @@ var options = { | ||
var proxy = proxyMiddleware('/api', options); | ||
var apiProxy = proxy('/api', options); | ||
// `/api/lorum/ipsum` -> `http://localhost:3000/extra/api/lorum/ipsum` | ||
``` |
@@ -10,3 +10,3 @@ # proxyTable | ||
var express = require('express'); | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
@@ -25,6 +25,6 @@ var proxyTable = { | ||
var proxy = proxyMiddleware('/', options); | ||
var myProxy = proxy('/', options); | ||
var app = express(); | ||
app.use(proxy); // add the proxy to express | ||
app.use(myProxy); // add the proxy to express | ||
@@ -31,0 +31,0 @@ app.listen(3000); |
@@ -13,3 +13,3 @@ # Recipes | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
var winston = require('winston'); | ||
@@ -48,3 +48,4 @@ | ||
proxyTable: { | ||
// <request> : <new target> | ||
// host[/path] : <new target> | ||
// /path : <new target> | ||
'integration.localhost:8000' : 'http://localhost:8001', // host only | ||
@@ -105,3 +106,2 @@ 'staging.localhost:8000' : 'http://localhost:8002', // host only | ||
}; | ||
@@ -112,3 +112,3 @@ | ||
*/ | ||
var proxy = proxyMiddleware(context, options); | ||
var apiProxy = proxy(context, options); | ||
``` |
@@ -8,8 +8,57 @@ # Shorthand | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
var proxy = proxyMiddleware('http://localhost:3000/api'); | ||
var apiProxy = proxy('http://localhost:3000/api'); | ||
// equivalent: | ||
// var proxy = proxyMiddleware('/api', {target:'http://localhost:3000'}); | ||
// var apiProxy = proxy('/api', {target:'http://localhost:3000'}); | ||
``` | ||
## Shorthand - Wildcard context | ||
This example will create a proxy middleware with shorthand wildcard context. | ||
```javascript | ||
var proxy = require("http-proxy-middleware"); | ||
var apiProxy = proxy('http://localhost:3000/api/books/*/**.json'); | ||
// equals: | ||
// var apiProxy = proxy('/api/books/*/**.json', {target:'http://localhost:3000'}); | ||
``` | ||
## Shorthand with additional configuration | ||
This example will create a proxy middleware with shorthand and additional configuration. | ||
```javascript | ||
var proxy = require("http-proxy-middleware"); | ||
var apiProxy = proxy('http://localhost:3000/api', {changeOrigin: true}); | ||
// equals: | ||
// var apiProxy = proxy('/api', {target:'http://localhost:3000', {changeOrigin:true}}); | ||
``` | ||
## Shorthand - WebSocket | ||
This example will create a proxy middleware with shorthand and additional configuration for WebSocket support. | ||
```javascript | ||
var proxy = require("http-proxy-middleware"); | ||
var apiProxy = proxy('http://localhost:3000/api', {ws: true}); | ||
// equals: | ||
// var apiProxy = proxy('/api', {target:'http://localhost:3000', ws: true}); | ||
``` | ||
## Shorthand - WebSocket only | ||
This example will create a proxy middleware with websocket shorthand only configuration. | ||
```javascript | ||
var proxy = require("http-proxy-middleware"); | ||
var apiProxy = proxy('ws://localhost:3000/api'); | ||
// equals: | ||
// var apiProxy = proxy('/api', {target:'ws://localhost:3000', ws: true}); | ||
``` |
@@ -6,5 +6,37 @@ # WebSocket | ||
```javascript | ||
var proxyMiddleware = require("http-proxy-middleware"); | ||
var proxy = require("http-proxy-middleware"); | ||
var proxy = proxyMiddleware('/socket', {target: 'http://localhost:3000', ws: true}); | ||
var socketProxy = proxy('/socket', {target: 'http://localhost:3000', ws: true}); | ||
``` | ||
## WebSocket - Path Rewrite | ||
This example will create a proxy middleware with websocket support and pathRewrite. | ||
```javascript | ||
var proxy = require("http-proxy-middleware"); | ||
var options = { | ||
target: 'http://localhost:3000', | ||
ws: true, | ||
pathRewrite: { | ||
'^/socket' : '' | ||
} | ||
}; | ||
var socketProxy = proxy('/socket', options); | ||
``` | ||
## WebSocket - Server update subscription | ||
This example will create a proxy middleware with websocket support. | ||
Subscribe to server's upgrade event. | ||
```javascript | ||
var proxy = require("http-proxy-middleware"); | ||
var socketProxy = proxy('/socket', {target: 'http://localhost:3000', ws: true}); | ||
server.on('upgrade', proxy.upgrade); // <-- subscribe to http 'upgrade' | ||
``` |
@@ -6,6 +6,7 @@ var expect = require('chai').expect; | ||
var result; | ||
var createConfig = configFactory.createConfig; | ||
describe('createConfig()', function() { | ||
describe('classic api', function() { | ||
describe('classic config', function() { | ||
var context = '/api'; | ||
@@ -15,14 +16,14 @@ var options = {target: 'http://www.example.org'}; | ||
beforeEach(function() { | ||
result = configFactory.createConfig(context, options); | ||
result = createConfig(context, options); | ||
}); | ||
it('should return on config object', function() { | ||
it('should return config object', function() { | ||
expect(result).to.have.all.keys('context', 'options'); | ||
}); | ||
it('should return on config object with context', function() { | ||
it('should return config object with context', function() { | ||
expect(result.context).to.equal(context); | ||
}); | ||
it('should return on config object with options', function() { | ||
it('should return config object with options', function() { | ||
expect(result.options).to.deep.equal(options); | ||
@@ -32,76 +33,92 @@ }); | ||
describe('shorthand api', function() { | ||
beforeEach(function() { | ||
result = configFactory.createConfig('http://www.example.org:8000/api'); | ||
}); | ||
describe('shorthand String', function() { | ||
describe('shorthand String config', function() { | ||
beforeEach(function() { | ||
result = createConfig('http://www.example.org:8000/api'); | ||
}); | ||
it('should return on config object', function() { | ||
expect(result).to.have.all.keys('context', 'options'); | ||
}); | ||
it('should return config object', function() { | ||
expect(result).to.have.all.keys('context', 'options'); | ||
}); | ||
it('should return on config object with context', function() { | ||
expect(result.context).to.equal('/api'); | ||
}); | ||
it('should return config object with context', function() { | ||
expect(result.context).to.equal('/api'); | ||
}); | ||
it('should return on config object with options', function() { | ||
expect(result.options).to.deep.equal({target: 'http://www.example.org:8000'}); | ||
it('should return config object with options', function() { | ||
expect(result.options).to.deep.equal({target: 'http://www.example.org:8000'}); | ||
}); | ||
}); | ||
}); | ||
describe('shorthand api for whole domain', function() { | ||
beforeEach(function() { | ||
result = configFactory.createConfig('http://www.example.org:8000'); | ||
}); | ||
describe('shorthand String config for whole domain', function() { | ||
beforeEach(function() { | ||
result = createConfig('http://www.example.org:8000'); | ||
}); | ||
it('should return on config object with context', function() { | ||
expect(result.context).to.equal('/'); | ||
it('should return config object with context', function() { | ||
expect(result.context).to.equal('/'); | ||
}); | ||
}); | ||
}); | ||
describe('shorthand api for websocket url', function() { | ||
beforeEach(function() { | ||
result = configFactory.createConfig('ws://www.example.org:8000'); | ||
}); | ||
describe('shorthand String config for websocket url', function() { | ||
beforeEach(function() { | ||
result = createConfig('ws://www.example.org:8000'); | ||
}); | ||
it('should return on config object with context', function() { | ||
expect(result.context).to.equal('/'); | ||
}); | ||
it('should return config object with context', function() { | ||
expect(result.context).to.equal('/'); | ||
}); | ||
it('should return on options with ws = true', function() { | ||
expect(result.options.ws).to.equal(true); | ||
it('should return options with ws = true', function() { | ||
expect(result.options.ws).to.equal(true); | ||
}); | ||
}); | ||
}); | ||
describe('shorthand api for secure websocket url', function() { | ||
beforeEach(function() { | ||
result = configFactory.createConfig('wss://www.example.org:8000'); | ||
describe('shorthand String config for secure websocket url', function() { | ||
beforeEach(function() { | ||
result = createConfig('wss://www.example.org:8000'); | ||
}); | ||
it('should return config object with context', function() { | ||
expect(result.context).to.equal('/'); | ||
}); | ||
it('should return options with ws = true', function() { | ||
expect(result.options.ws).to.equal(true); | ||
}); | ||
}); | ||
it('should return on config object with context', function() { | ||
expect(result.context).to.equal('/'); | ||
describe('shorthand String config with globbing', function() { | ||
beforeEach(function() { | ||
result = createConfig('http://www.example.org:8000/api/*.json'); | ||
}); | ||
it('should return config object with context', function() { | ||
expect(result.context).to.equal('/api/*.json'); | ||
}); | ||
}); | ||
it('should return on options with ws = true', function() { | ||
expect(result.options.ws).to.equal(true); | ||
describe('shorthand String config with options', function() { | ||
beforeEach(function() { | ||
result = createConfig('http://www.example.org:8000/api', {changeOrigin: true}); | ||
}); | ||
it('should return config object with additional options', function() { | ||
expect(result.options).to.deep.equal({target: 'http://www.example.org:8000', changeOrigin: true}); | ||
}); | ||
}); | ||
}); | ||
describe('shorthand api with globbing', function() { | ||
describe('shorthand Object config', function() { | ||
beforeEach(function() { | ||
result = configFactory.createConfig('http://www.example.org:8000/api/*.json'); | ||
result = createConfig({target: 'http://www.example.org:8000'}); | ||
}); | ||
it('should return on config object with context', function() { | ||
expect(result.context).to.equal('/api/*.json'); | ||
it('should set the proxy path to everything', function() { | ||
expect(result.context).to.equal('/'); | ||
}); | ||
}); | ||
describe('shorthand api with options', function() { | ||
beforeEach(function() { | ||
result = configFactory.createConfig('http://www.example.org:8000/api', {changeOrigin: true}); | ||
it('should return config object', function() { | ||
expect(result.options).to.deep.equal({target: 'http://www.example.org:8000'}); | ||
}); | ||
it('should return on config object with additional options', function() { | ||
expect(result.options).to.deep.equal({target: 'http://www.example.org:8000', changeOrigin: true}); | ||
}); | ||
}); | ||
@@ -113,3 +130,3 @@ | ||
fn = function() { | ||
configFactory.createConfig('/api'); | ||
createConfig('/api'); | ||
}; | ||
@@ -126,3 +143,3 @@ }); | ||
beforeEach(function() { | ||
result = configFactory.createConfig('http://localhost:3000/api', {target: 'http://localhost:8000'}); | ||
result = createConfig('http://localhost:3000/api', {target: 'http://localhost:8000'}); | ||
}); | ||
@@ -129,0 +146,0 @@ |
@@ -23,3 +23,3 @@ var expect = require('chai').expect; | ||
var mockReq = {url: '/foo/bar'}; | ||
var mockReq = {url: '/foo/bar', originalUrl: '/foo/bar'}; | ||
var mockRes = {}; | ||
@@ -26,0 +26,0 @@ var mockNext = function() { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
145396
2220
382
11
44