-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Description
Problem
express
mutates req.url
, and saves the un-mutated version onto originalUrl
, making URL driven generic middleware hard to design.
Ref: chimurai/http-proxy-middleware#723
Apologies if this topic has already been discussed, I did execute a cursory search and didn't see it, but I miss things time to time.
Context
http.IncomingMessage
is the http
module's owned memory, and thus could be considered memory not open for writing. The writing to this memory prevents me from fixing problematic typescript issues in http-proxy-middleware
. To make http-proxy-middleware
generic is to decouple it from all frameworks that it supports--express, connect, raw http, koa, fastify, ...the works!
However, because express mutates req.url, I cannot trust http.IncomingMessage::url
, and either have to:
- burden users with passing to HPM the same prefix they use in
app.use(prefix, ...)
, then additionally track that prefix internally to the HPM lib, or - have
express
conditionals within HPM that sniff specifically for indicators of this framework, and use alternate logic.- this is the current state of http-proxy-middleware, but to make it play nicely with other server providers, we're now trying to avoid this
Solutions
- write to new, owned memory only:
// GET /api/foo/bar
req.url // untouched, /api/foo/bar
app.use('/api', ...)
req.express.url // /foo/bar
req.express.base // /api
-
don't mutate URL at all. offer functions for users to derive relative path to the base/prefix path
-
offer a
.relativePathname
, yieldingreq.url
-sum(...nestedPrefixes)