http-proxy-middleware
Advanced tools
Comparing version 0.6.0 to 0.7.0
28
index.js
var _ = require('lodash'); | ||
var httpProxy = require('http-proxy'); | ||
var configFactory = require('./lib/config-factory'); | ||
var handlers = require('./lib/handlers'); | ||
@@ -10,20 +11,8 @@ var contextMatcher = require('./lib/context-matcher'); | ||
var isWsUpgradeListened = false; | ||
var proxyOptions = opts || {}; | ||
var config = configFactory.createConfig(context, opts); | ||
var proxyOptions = config.options; | ||
// Legacy option.proxyHost | ||
// set options.headers.host when option.proxyHost is provided | ||
if (proxyOptions.proxyHost) { | ||
console.log('*************************************'); | ||
console.log('[HPM] Deprecated "option.proxyHost"'); | ||
console.log(' Use "option.changeOrigin" or "option.headers.host" instead'); | ||
console.log(' "option.proxyHost" will be removed in future release.'); | ||
console.log('*************************************'); | ||
proxyOptions.headers = proxyOptions.headers || {}; | ||
proxyOptions.headers.host = proxyOptions.proxyHost; | ||
} | ||
// create proxy | ||
var proxy = httpProxy.createProxyServer(proxyOptions); | ||
console.log('[HPM] Proxy created:', context, ' -> ', proxyOptions.target); | ||
console.log('[HPM] Proxy created:', config.context, ' -> ', proxyOptions.target); | ||
@@ -60,3 +49,8 @@ var pathRewriter = PathRewriter.create(proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided | ||
function middleware (req, res, next) { | ||
if (contextMatcher.match(context, req.url)) { | ||
// https://github.com/chimurai/http-proxy-middleware/issues/17 | ||
if (req.baseUrl) { | ||
req.url = req.originalUrl; | ||
} | ||
if (contextMatcher.match(config.context, req.url)) { | ||
if (proxyOptions.proxyTable) { | ||
@@ -87,3 +81,3 @@ // change option.target when proxyTable present. | ||
req.connection.server.on('upgrade', function (req, socket, head) { | ||
if (contextMatcher.match(context, req.url)) { | ||
if (contextMatcher.match(config.context, req.url)) { | ||
if (pathRewriter) { | ||
@@ -90,0 +84,0 @@ req.url = pathRewriter(req.url); |
module.exports = { | ||
proxyError : proxyError | ||
proxyError : proxyError | ||
} | ||
@@ -4,0 +4,0 @@ |
{ | ||
"name": "http-proxy-middleware", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync", | ||
@@ -33,12 +33,12 @@ "main": "index.js", | ||
"devDependencies": { | ||
"browser-sync": "^2.7.13", | ||
"chai": "^3.0.0", | ||
"browser-sync": "^2.8.2", | ||
"chai": "^3.2.0", | ||
"connect": "^3.4.0", | ||
"coveralls": "^2.11.2", | ||
"express": "^4.13.1", | ||
"istanbul": "^0.3.17", | ||
"coveralls": "^2.11.4", | ||
"express": "^4.13.3", | ||
"istanbul": "^0.3.18", | ||
"istanbul-coveralls": "^1.0.3", | ||
"mocha": "^2.2.5", | ||
"mocha-lcov-reporter": "0.0.2", | ||
"ws": "^0.7.2" | ||
"ws": "^0.8.0" | ||
}, | ||
@@ -45,0 +45,0 @@ "dependencies": { |
@@ -24,4 +24,7 @@ # http-proxy-middleware | ||
// 'proxy' is now ready to be used in a server. | ||
// 'proxy' is now ready to be used in a server. | ||
// shorthand syntax for the example above: | ||
// proxyMiddleware('http://www.example.org/api'); | ||
``` | ||
@@ -43,4 +46,6 @@ * **context**: matches provided context against request-urls' path. | ||
// configure proxy middleware | ||
// configure proxy middleware context | ||
var context = '/api'; // requests with this path will be proxied | ||
// configure proxy middleware options | ||
var options = { | ||
@@ -69,3 +74,3 @@ target: 'http://www.example.org', // target host | ||
See [working examples](#more-examples). | ||
Check out [working examples](#more-examples). | ||
@@ -97,2 +102,18 @@ **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`. | ||
### Shorthand | ||
Use the shorthand syntax for simple use cases. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed. | ||
```javascript | ||
proxyMiddleware('http://www.example.org:8000/api'); | ||
// proxyMiddleware('/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'}); | ||
proxyMiddleware('http://www.example.org:8000/api', {changeOrigin:true}); | ||
// proxyMiddleware('/api', {target: 'http://www.example.org:8000', changeOrigin: true}); | ||
``` | ||
### Options | ||
@@ -201,3 +222,4 @@ * **option.pathRewrite**: object, rewrite target's url path. Object-keys will be used as _RegExp_ to match paths. | ||
### Changlog | ||
### Changelog | ||
* [v0.7.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.7.0) - support shorthand syntax, fixed express/connect mounting | ||
* [v0.6.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.6.0) - support proxyTable | ||
@@ -204,0 +226,0 @@ * [v0.5.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.5.0) - support subscribing to http-proxy error- and proxyRes-event |
@@ -465,10 +465,78 @@ var expect = require('chai').expect; | ||
describe('shorthand usage', function () { | ||
var proxyServer, targetServer; | ||
var responseBody; | ||
beforeEach(function (done) { | ||
var mw_proxy = proxyMiddleware('http://localhost:8000/api'); | ||
var mw_target = function (req, res, next) { | ||
res.write(req.url); // respond with req.url | ||
res.end(); | ||
}; | ||
proxyServer = createServer(3000, mw_proxy); | ||
targetServer = createServer(8000, mw_target); | ||
http.get('http://localhost:3000/api/foo/bar', function (res) { | ||
res.on('data', function (chunk) { | ||
responseBody = chunk.toString(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
afterEach(function () { | ||
proxyServer.close(); | ||
targetServer.close(); | ||
}); | ||
it('should have proxy with shorthand configuration', function () { | ||
expect(responseBody).to.equal('/api/foo/bar'); | ||
}); | ||
}); | ||
describe('express with path + proxy', function () { | ||
var proxyServer, targetServer; | ||
var responseBody; | ||
beforeEach(function (done) { | ||
var mw_proxy = proxyMiddleware('http://localhost:8000'); | ||
var mw_target = function (req, res, next) { | ||
res.write(req.url); // respond with req.url | ||
res.end(); | ||
}; | ||
proxyServer = createServer(3000, mw_proxy, '/api'); | ||
targetServer = createServer(8000, mw_target); | ||
http.get('http://localhost:3000/api/foo/bar', function (res) { | ||
res.on('data', function (chunk) { | ||
responseBody = chunk.toString(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
afterEach(function () { | ||
proxyServer.close(); | ||
targetServer.close(); | ||
}); | ||
it('should proxy to target with the baseUrl', function () { | ||
expect(responseBody).to.equal('/api/foo/bar'); | ||
}); | ||
}); | ||
}); | ||
function createServer (portNumber, middleware) { | ||
function createServer (portNumber, middleware, path) { | ||
var app = express(); | ||
if (middleware) { | ||
if (middleware, path) { | ||
console.log('pathpathpathpathpathpathpath: ', path); | ||
app.use(path, middleware); | ||
} | ||
else if (middleware) { | ||
app.use(middleware); | ||
@@ -475,0 +543,0 @@ } |
75636
23
1412
251