QUESTION:
Is there any way to create a variable storage in per session/http request? The variable must be globally accessible and different
Per request, I think what you're after could be done with ordinary closures. For example, you'd define your custom functions in module that takes a req
argument:
util_funcs.js:
module.exports = function( req ){
return {
customFunctionThatRequiresReq: function(){ console.info( req ) },
otherFunctionThatRequiresReq: function(){ console.log( req ) }
};
};
Then wherever you depend on those functions (presumably some middleware elsewhere in the application), you can just require them in context:
var someMiddleWare = function( req, res, next ){
var utils = require( 'util_funcs.js' )( req );
utils.customFunctionThatRequiresReq();
utils.otherFunctionThatRequiresReq();
};
This allows you to avoid littering your function args with req
, and no dubious globals.