for data that only needs to be available during an individual request, where should it be stored? i am creating new properties on the req and res objects so i dont have to pass that data from function to function.
req.myNewValue = 'just for this request'
is the process object an option? or is it shared globally across all requests?
If you are talking about the variable passed like here:
http.createServer(function (req, res) {
req.myNewValue = 'just for this request';
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
then it is perfectly fine what you are doing. req
stores the request data, you can modify it as you want. If you are using some framework like Express, then it should be fine as well (keep in mind that you may override some built-in properties of req
object).
If by "process object" you are refering to the global variable process
, then absolutely not. The data here is global and shouldn't be modified at all.
You should be creating a new variable with all of the information you need, per request. Otherwise, you risk colliding with new features added in the future.
Node.js applications do not have fresh copies of them created with each request, meaning that the process object (and all others in scope) are shared.