chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.79k stars 852 forks source link

req.url is always '/' #1000

Closed satanshiro closed 6 months ago

satanshiro commented 6 months ago

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 commented 6 months ago

Same problem

chimurai commented 6 months ago

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 commented 6 months ago

@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 commented 6 months ago

Maybe a bit hidden... It's already documented here: https://github.com/chimurai/http-proxy-middleware?tab=readme-ov-file#express-server-example https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md#nextjs

I'll see how I can improve the docs 👍