5

I am trying to get variables that I can get everywhere in my code I found a solution but that's not very clean

//environment.js    
module.exports.appName = "appName";

and my app.js

var express = require('express')
, routes = require('./routes/main')
, user = require('./routes/user')
, http = require('http')
, path = require('path');

var app = express();

environment = require('./environment');

app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});


app.get('/', routes.home);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

In this way my var works, I can access environment.appName everywhere, but Y would have better solution

Thanks

3
  • IMHO global variables are a bad solution in gerneral. So there probably is no "clean solution" for a bad thing to do. Commented Nov 14, 2012 at 14:51
  • 1
    In the case of the Database connection (initialized at startup and only one object though the entire lifetime of the server) this is a good approach. Commented Nov 6, 2013 at 16:53
  • Glad you posted this, thanks... helped out a bit. Switching to node right now and trying to figure stuff out. Still not sure what the app.configure('development', function(){}); does even though I'm using it. Does that set the environment to development and then just comment that out when pushing to production? Commented Dec 8, 2013 at 22:59

1 Answer 1

10

There is a global scope in node.js.

In the main file (the one you put behind node in command line,) all variables defined are in global scope.

var x = 100;
// which global.x === 100

And in other files loaded as module, the scope is not global but a local sandbox object, so you need global.x to access the same x in main file.

Seems it looks better than use a require().

Sign up to request clarification or add additional context in comments.

2 Comments

I believe you have to omit the var for it to be under the global object otherwise it's a variable of the scope of the module. Alternatively you can always be explicit by saying global.x = 100;
@ThalisKalfigkopoulos Thanks for pointing out that. I assumed everyone read it could infer that from the context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.