Skip to content

Commit 5188acd

Browse files
committed
Merge pull request #18 from chimurai/baseurl
Support mounting when connect/express path is configured.
2 parents 0cf17e2 + 6aec081 commit 5188acd

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ var httpProxyMiddleware = function (context, opts) {
4545
return middleware;
4646

4747
function middleware (req, res, next) {
48+
// https://github.com/chimurai/http-proxy-middleware/issues/17
49+
if (req.baseUrl) {
50+
req.url = req.originalUrl;
51+
}
52+
4853
if (contextMatcher.match(config.context, req.url)) {
4954
if (proxyOptions.proxyTable) {
5055
// change option.target when proxyTable present.

test/http-proxy-middleware.spec.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,50 @@ describe('http-proxy-middleware in actual server', function () {
494494
});
495495
});
496496

497+
describe('express with path + proxy', function () {
498+
var proxyServer, targetServer;
499+
var responseBody;
500+
501+
beforeEach(function (done) {
502+
var mw_proxy = proxyMiddleware('http://localhost:8000');
503+
var mw_target = function (req, res, next) {
504+
res.write(req.url); // respond with req.url
505+
res.end();
506+
};
507+
508+
proxyServer = createServer(3000, mw_proxy, '/api');
509+
targetServer = createServer(8000, mw_target);
510+
511+
http.get('http://localhost:3000/api/foo/bar', function (res) {
512+
res.on('data', function (chunk) {
513+
responseBody = chunk.toString();
514+
done();
515+
});
516+
});
517+
});
518+
519+
afterEach(function () {
520+
proxyServer.close();
521+
targetServer.close();
522+
});
523+
524+
it('should proxy to target with the baseUrl', function () {
525+
expect(responseBody).to.equal('/api/foo/bar');
526+
});
527+
528+
});
497529

498530
});
499531

500532

501-
function createServer (portNumber, middleware) {
533+
function createServer (portNumber, middleware, path) {
502534
var app = express();
503535

504-
if (middleware) {
536+
if (middleware, path) {
537+
console.log('pathpathpathpathpathpathpath: ', path);
538+
app.use(path, middleware);
539+
}
540+
else if (middleware) {
505541
app.use(middleware);
506542
}
507543

0 commit comments

Comments
 (0)