I want to store information along with a Node.js request (IncomingMessage
). I'd like to do it with a WeakMap like this:
const infoByRequest = new WeakMap()
// somewhere in the request handler
const setInfo = (req, info) => {
infoByRequest.set(req, info)
}
const getInfo = (req) => {
return infoByRequest.get(req)
}
Is this safe? I was wondering if the req object instance eventually gets reused and thus may not work as expected.