Skip to content

Commit

Permalink
feat(pathRewrite): expose the req object (#76)
Browse files Browse the repository at this point in the history
* feat(pathRewrite): expose the `req` object
* chore(tests): split proxyTable in unit & e2e tests
  • Loading branch information
chimurai committed May 11, 2016
1 parent b52c14b commit cbb2f61
Show file tree
Hide file tree
Showing 16 changed files with 761 additions and 644 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# Changelog

## [v0.15.x](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.15.x)
- feat(pathRewrite): expose `req` object to pathRewrite function.

## [v0.15.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.15.0)
- feat(pathRewrite): support for custom pathRewrite function.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -176,7 +176,7 @@ Providing an alternative way to decide which requests should be proxied; In case
pathRewrite: {'^/' : '/basepath/'}

// custom rewriting
pathRewrite: function (path) { return path.replace('/api', '/base/api') }
pathRewrite: function (path, req) { return path.replace('/api', '/base/api') }
```

* **option.proxyTable**: object, re-target `option.target` based on the request header `host` parameter. `host` can be used in conjunction with `path`. Only one instance of the proxy will be used. The order of the configuration matters.
Expand Down
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -81,7 +81,7 @@ var httpProxyMiddleware = function(context, opts) {
// 1. option.proxyTable
// 2. option.pathRewrite
var alteredProxyOptions = __applyProxyTableOption(req, proxyOptions);
__applyPathRewrite(req, pathRewriter);
__applyPathRewrite(pathRewriter, req);

// debug logging for both http(s) and websockets
if (proxyOptions.logLevel === 'debug') {
Expand All @@ -105,9 +105,9 @@ var httpProxyMiddleware = function(context, opts) {
}

// rewrite path
function __applyPathRewrite(req) {
function __applyPathRewrite(pathRewriter, req) {
if (pathRewriter) {
var path = pathRewriter(req.url);
var path = pathRewriter(req.url, req);

if (path) {
req.url = path;
Expand Down
2 changes: 1 addition & 1 deletion recipes/pathRewrite.md
Expand Up @@ -78,7 +78,7 @@ The unmodified path will be used, when rewrite function returns `undefined`
```javascript
var proxy = require("http-proxy-middleware");

var rewriteFn = function (path) {
var rewriteFn = function (path, req) {
return path.replace('/api/foo', '/api/bar');
}

Expand Down
21 changes: 21 additions & 0 deletions test/e2e/_utils.js
@@ -0,0 +1,21 @@
var express = require('express');
var proxyMiddleware = require('../../index');

module.exports = {
createServer: createServer,
proxyMiddleware: proxyMiddleware
};

function createServer(portNumber, middleware, path) {
var app = express();

if (middleware, path) {
app.use(path, middleware);
} else if (middleware) {
app.use(middleware);
}

var server = app.listen(portNumber);

return server;
}

0 comments on commit cbb2f61

Please sign in to comment.