node.js - How exactly does eval work with nodejs? -
let's do:
eval( db_config = { host: 'localhost', user: 'root', database: 'forum', password: 'test' } ); var gamefunctions = require('gamefunctions.js');
i can use db_config
anywhere inside gamefunctions.js
without having pass through parameter. that's pretty neat. but, bad practice?
reason ask because if do:
var db_config = { host: 'localhost', user: 'root', database: 'forum', password: 'test' } var gamefunctions = require('gamefunctions.js');
db_config
becomes undefined anytime use in gamefunctions.js
. , have pass through parameter on each different function seems evaling
first save time , code, downside this?
does eval
define variables in global scope you, can used in file in nodejs?
you're doing 2 things wrong , there's more elegant solution this.
to explain you're doing wrong, first use of globals. second you're storing sensitive information in code - database password! , besides, won't portable. suppose want run on machine has other database credentials you'll end changing code unnecessarily.
you need store environment variables. , if end hosting environments automatically have set use them in app. example openshift have $openshift_mongodb_db_host
tells use in app connect database.
and need create separate file store db_config
can require other files.
so might create db_config.js
this
var config = {}; config.host = process.env.host || 'localhost'; config.user = process.env.user || 'root'; config.password = process.env.password; module.exports = config;
then can safely pass sensitive information password console
$ password=pass $ node index.js
and having information in gamefunctions.js
gotta require it
var db_config = require('./db_config');
Comments
Post a Comment