Skip to content

path regression (trailing slash is added in 0.5.2) #138

@chimurai

Description

@chimurai

Environment

Hi,

Tried to update to 0.5.2 in http-proxy-middleware

Looks like #137 introduced regression in paths?

In this specific test /api is not preserved and is now being proxied differently:
https://github.com/chimurai/http-proxy-middleware/pull/1234/changes#diff-89634a91bb39f03a55c0c5ffa685aa8bc2c807017ae554f83caa4cd25cf50a95R160

The url is now send as /api/ to target server. (with trailing slash)

Reproduction

const app = express()

app.use('/', createProxyMiddleware({
    changeOrigin: true,
    target: `http://localhost:9000/api`,        // <--- configure target with path
}));

app.listen(3000)

before: (0.5.1)
curl http://localhost:3000 -> http://localhost:9000/api

after: (0.5.2)
curl http://localhost:3000 -> http://localhost:9000/api/

                                                       ^ Notice the trailing slash

Describe the bug

Trailing slash is added when target is used with path


If I revert the logic from (#137) to the previous, the trailing slash is gone.

function joinURL(base, path) {
	if (!base || base === "/") return path || "/";
  if (!path || path === "/") {     // <---- original
    return base || "/";            // <---- original
  }

  //...
}

Additional context

Recently migrated to httpxy in https://www.npmjs.com/package/http-proxy-middleware

The { target: 'http://[::1]:9000/api' } is quite common pattern to rewrite paths when the proxy is mounted on same or different path in the server.

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions