Domenico Vacchiano
Domenico Vacchiano

Reputation: 136

NodeJS export class with static methods

I’m trying to develop a class with static methods on a NodeJs application, as a Config module purpose.
I would like to access to it from different modules without instantiate every time a new object.

1) Is it correct to use an approach like below, avoiding the class prototype?

function Config(){

}

Config.svrPort=function(){
    return 8080;
}

Config.dbName=function(){
    return "myDbName";
}

module.exports = Config;

2) Are there any other solutions?

3) Is it also a valid approach to put different objects in the same module (e.g. config.js) like this?

exports.server=function(){
    return{
        port:8080
    };
};

exports.database=function(){
    return{
        name:"myDbName",
        user:"root",
        password:"myPassword",
        host:"localhost",
        port:3306
    };
};

and finally to use it as:

var config = require('./config');
config.server().port
config.database().name

Upvotes: 5

Views: 7370

Answers (3)

Andreas Louv
Andreas Louv

Reputation: 47099

That is the correct approach although in your example you could simply store the values as primitives: Config.SVR_PORT = 8080, note I rewrote those as "constants", since I don't recommend changing statics.

When this is said, then please note: Do not put any sensitive data (passwords, etc) in your JavaScript files, but instead put these in a config file. This will separate config from code. And help you not to leak any potential critical information.

db-secrets.json

{
  "password": "...",
  ...
}

Then you can access the secrets by making a wrapper module:

// Wrapper module for db-secrets.json
var fs = require('fs');
exports = JSON.parse(fs.readFileSync('db-secrets.json'), 'utf8');

Or make a db connect module db-connect.js:

var fs = require('fs');
var db = require('db');
var db_secrets = JSON.parse(fs.readFileSync('db-secrets.json'), 'utf8');

export = function() {
  return db.connect(db_secrets.user, db_secrets.password, ...);
};

If you use git for source control you can easily add the following to your .gitignore which will make sure your sensitive file is not added to git:

.gitignore

db-secrets.json

Upvotes: 2

Vidul
Vidul

Reputation: 10528

There are no classes in the prototype languages (it's just a syntax sugar that mimics the classical OOP). Hence no static methods. My guess is that you want a singleton (which is the same as object literal).

var Config = {
    get svrPort() {
        return 8080;
    },

    get dbName() {
        return "myDbName";
    },
};

// updated: https://nodejs.org/api/modules.html#modules_caching
// module.exports = Object.create(Config);
module.exports = Config;

Upvotes: 1

Tracker1
Tracker1

Reputation: 19334

JS supports object literals, not to mention that you can export a single object, or multiple export properties on the default exports object... When the require('./your-module'); happens the code in the module is not run again, it simply returns the original export, that has it's own original context.


Just export each of the functions/variables that you want as either an object literal, or attached to the exports.

//just declare any private context as it's own variables in your module, these are static
var someVal = "this isn't visible outside the module";

//export single object, with methods attached
//  NOTE: by default exports exists, and is already module.exports
//  exports === module.exports
exports = module.exports = { 
  getSomeVal: function getSomeVal(){
    return someVal;
  },
  getSrvPort: function getSrvPort(){
    return 8000;
  }
  ...
}

//alternatively, export each method as property
//    note, you should name your function assignments, 
//    this helps with debugging.
exports.getDbName = function getDbName(){
  return "mydb";
};

The code only actually runs once and all places using it will see the same option.


As an aside, if you're using Babel with ES6 modules, you can simply declare your export with each method...

export function getSrvPort() {
  return 8000;
}

export function dbName() {
  return "mydb";
}

Upvotes: 1

Related Questions