Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variable express node.js

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

like image 621
Ajouve Avatar asked Nov 14 '12 14:11

Ajouve


People also ask

How do you call a global variable in node JS?

Just because you use the word var at the top of your Node. js script does not mean the variable will be accessible by all objects you require such as your 'basic-logger' . To make something global just put the word global and a dot in front of the variable's name.

How do I create a global variable in Express?

To make a global variable, just declare it without the var keyword. (Generally speaking this isn't best practice, but in some cases it can be useful - just be careful as it will make the variable available everywhere.) //we can now access 'app' without redeclaring it or passing it in... /* * GET home page. */ app.

What is global variable in node JS?

Global variables are variables that can be declared with a value, and which can be accessed anywhere in a program. The scope of global variables is not limited to the function scope or any particular JavaScript file. It can be declared at one place and then used in multiple places.

Is using global variables bad in nodeJS?

Global variables are considered an anti-pattern in almost any programming language because they make it very hard to follow and debug code. When you browse the code, you never know which function sets or uses a global variable.


1 Answers

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().

like image 97
xiaoyi Avatar answered Sep 25 '22 23:09

xiaoyi