Answer the question
In order to leave comments, you need to log in
What is the correct way to write a module for node.js?
Started learning node.js, a beginner's question on module design.
I want to move all application configs into a separate module. Here's a call to conf.redisHost() that works. But this one - conf.redisHost - is not. Is there a way to describe config.js to get config options without parentheses?
Code example.
// a.js
var conf = require('./lib/config');
console.log(conf.redisHost); // [Function]
console.log(conf.redisHost()); // 127.0.0.1
// config.js
module.exports = new Сonfig;
function Сonfig(){};
Сonfig.prototype.redisHost = function(){
return (process.env.REDIS_HOST || '127.0.0.1');
};
Answer the question
In order to leave comments, you need to log in
Are you sure you need to learn nodeJS, and not javascript in general?
// config.js
exports.redisHost = (process.env.REDIS_HOST || '127.0.0.1');
// a.js
var conf = require('./lib/config');
console.log(conf.redisHost); // 127.0.0.1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question