Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139863

Is it safe to use a Node.js request (req) as key to a WeakMap

$
0
0

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.


Viewing all articles
Browse latest Browse all 139863

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>