Skip to content

attach req as res.req#36505

Merged
aduh95 merged 1 commit intonodejs:masterfrom
ianstormtaylor:add-res-to-req-link
Jan 18, 2021
Merged

attach req as res.req#36505
aduh95 merged 1 commit intonodejs:masterfrom
ianstormtaylor:add-res-to-req-link

Conversation

@ianstormtaylor
Copy link
Contributor

@ianstormtaylor ianstormtaylor commented Dec 14, 2020

This change adds a res.req property which refers to the original IncomingMessage request object.

The reasons for this small change are explained in #28673, but a quick summary:

  • All existing Node HTTP frameworks do this under the covers when wrapping http.createServer.
  • Referencing the original request is required to handle certain HTTP edge cases when writing responses.
  • Node's internals already reference the req in multiple places when determining response-specific behavior.
  • Retrieving the res from the req is already possible, this just adds the reverse.
  • This would give userland the ability to easily handle these valid HTTP-related edge cases.
  • This would allow for intuitive Lodash-like HTTP helper libraries, instead of relying on monolithic Express-like frameworks.

I'd recommend reading #28673 in full, I put a lot of thought/research into the different arguments.

You can imagine a world where you aren't forced to use frameworks for HTTP in Node, and instead we might benefit from a Lodash-like utility library with smaller, functional utilities…

image

…this would be helpful not only to those who don't want to use frameworks, but also those attempting to building framework-independent libraries and utilities, which is currently almost impossible in the Node ecosystem.

Thanks!


Also, to be clear, I'm not wed to the res.req naming scheme if it needs to be changed for any reason. As long as there's a way to access the original request I don't care what the way is.

Checklist
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. http Issues or PRs related to the http subsystem. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants