0

We can create a variables with req. (req.foo = bar)at front, so that it can be accessed by different middlewares. When are these variables destroyed?

Anson Ng
  • 11
  • 2
  • The `req` object (and all its variables like `req.foo`) is created per request. A new request gets a new `req` object. Does that answer your question? – Heiko Theißen Dec 10 '22 at 12:35
  • You better use `res.locals` for this: https://stackoverflow.com/questions/33451053/req-locals-vs-res-locals-vs-res-data-vs-req-data-vs-app-locals-in-express-mi – bill.gates Dec 10 '22 at 12:50
  • Just the same as the req object that it attched to. But I'd use word 'abandoned' instead of "destroyed'. (is it considered to be 'destroyed' when it is overwritten or deleted? ) – Todd Wong Dec 10 '22 at 13:25

1 Answers1

0

In Express.js, the req object (short for request) is created when a request is made to the server, and it is destroyed after the response is sent back to the client. This means that any variables that are attached to the req object will be available during the processing of the request, but they will be destroyed and no longer accessible once the response has been sent.

0xdw
  • 3,755
  • 2
  • 25
  • 40