Are the node.js built in objects available for adding custom properties?
var http = require('http');
var server = http.createServer(function(request, response) {
request.myObj = {'lots of info':true}; <-- is it ok to add this object to request?
response.writeHead(200, {
'Content-type':'text/plain'
});
response.end('Hello World!');
});
server.listen(8888);
console.log('Listening on http://127.0.0.1:8888');
Is this considered acceptable or off limits?
This is very commonly done, especially in express/connect apps. Just watch out for name collisions, but otherwise the node community seems mostly totally OK with this based on my experience. If you are paranoid, use a unique namespace like req.MY_APP = {};
and stick all your stuff there.
I did one time encounter a bug when both my application and the strongloop agent tried to set req.graph
, but I contacted them and they agreed they should use a less common name for their property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With