hapijs / hapi

The Simple, Secure Framework Developers Trust
https://hapi.dev
Other
14.63k stars 1.34k forks source link

Global variable #2568

Closed furyscript closed 9 years ago

furyscript commented 9 years ago

Hi,

I'm start with hapi-ninja boilerplate. I need to make accessible some variable. For example I create a service and I start ("construct", "load") it in the server.js (main file). I would that this variable so var user_service = require(__dirname + '/service/user.js'); is accessible from other service. For example:

server.js

var user_service = require(__dirname + '/service/user.js');
var test_service = require(__dirname + '/service/test.js');

user.js

module.exports.someFunction = function() {
   return 'Hello';
};

test.js

module.exports.useUserFunction = function() {
   return user_service.someFunction();
   // I got error because user_service is undefined
};

I solution that I'd found is load inside the test_service again user_service but it's bad because I'd load too much times user_service. What is the best solution for shared variables??

Marsup commented 9 years ago

This is not a hapi issue, use the discuss repo for that.