Skip to content

Support mounting when connect/express path is configured. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ var httpProxyMiddleware = function (context, opts) {
return middleware;

function middleware (req, res, next) {
// 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) {
// change option.target when proxyTable present.
Expand Down
40 changes: 38 additions & 2 deletions test/http-proxy-middleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,50 @@ describe('http-proxy-middleware in actual server', function () {
});
});

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);
}

Expand Down