Skip to content
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

req.url is always '/' #1000

Closed
2 tasks done
satanshiro opened this issue May 6, 2024 · 4 comments
Closed
2 tasks done

req.url is always '/' #1000

satanshiro opened this issue May 6, 2024 · 4 comments

Comments

@satanshiro
Copy link

Checks

Describe the bug (be clear and concise)

when upgrading http proxy middleware in nest i am unable to path rewrite using req.url
it always has the value '/'
in order for the path rewrite to work as before i need to access a variable called original url that is not strongly typed on the request object
this is my code:
image

Step-by-step reproduction instructions

1. create a nest module that implements NestModule
2. apply the new proxy middleware in the configure method. this is probably mounted on server path and not globally (nest implementation)
3. try to use path rewrite to achieve legacy behavior via pathRewrite: (path, req) => req.url
4. notice how all requests are still ignoring paths
5. on debugging you can see path.url is always '/' and there is an inaccessible variable called originalurl
6. using original url variable we can achieve required behavior

Expected behavior (be clear and concise)

one of 2 : req.url to contain the original url
or:
originalUrl variable to be avaialble in they request type (currently http.IncomingMessage)

How is http-proxy-middleware used in your project?

it is used as an post authentication proxy

What http-proxy-middleware configuration are you using?

target and path rewrite as in the example

What OS/version and node/version are you seeing the problem?

Maos 14.4.1 node v20.12.2

Additional context (optional)

No response

@SuhasQik
Copy link

SuhasQik commented May 7, 2024

Same problem

@chimurai
Copy link
Owner

The / is the behaviour from http-proxy. (should be fix upstream)

Regarding the typing issue. You can provide appropriate request and response type from your server:

import type { Request, Response, NextFunction } from 'express';

createProxyMiddleware<Request, Response>{
  target: config.env.TARGET_URL,
  changeOrigin: true,
  pathRewrite: (path, req) => req.originalUrl,
  logger: console
}

@satanshiro
Copy link
Author

@chimurai thank you! so i guess this is desired behavior, might be a good idea to add to the docs.
thank you and have a nice day

@chimurai
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants