F
F
fuabzop2015-12-08 14:59:23
Node.js
fuabzop, 2015-12-08 14:59:23

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

2 answer(s)
A
Alexander Wolf, 2015-12-08
@mannaro

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

A
Andrey Titov, 2019-06-14
@titov_andrei

Working with modules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question