Kyle
Kyle

Reputation: 2379

NodeJS Group Functions Under A Sub-Class

perhaps I have not worded the title correctly. Below is the explanation of what I'm trying to do.

I'm creating a helper.js file for my project. Inside it contains many functions, one I've pasted below. I export this function using module.exports.

function generateInternalError(message, stack_trace) {
    if (process.env.NODE_ENV == 'dev') {
        console.log({ message: message, stack_trace: stack_trace });
    } else {
        console.log({message: message});
    }
}

module.exports = {
    generateInternalError: generateInternalError
};

Where I want to utilize this function I would call:

helper.generateInternalError('Not Found',new Error().stack);

And it works as expected.

But, what I have been tasked with, is creating categories of functions. Essentially I need the following:

helper.errors.generateInternalError('Not Found',new Error().stack);

I can not seem to figure out the right way to export a class of functions or an object of functions in NodeJS such that I don't get an error like:

TypeError: helper.errors.generateClientError is not a function

Any assistance is appreciated.

Thank you

Upvotes: 0

Views: 662

Answers (3)

Jason Livesay
Jason Livesay

Reputation: 6377

helpers is just noise if everything is a helper, so drop that. Just use regular modules and unless you are sure you only have one function in that category export multiple functions. If you only export one function with module.exports then you don't need to do that as a property of an object which also means you can just say const genError=require('./errors')

Don't make something like helpers.errors.someErrorFunc because helpers is noise and you make categories with just separate module files. Don't try to make Node.js look like Java or something equally horrible.

Upvotes: 1

Eric Uldall
Eric Uldall

Reputation: 2391

It might be better to structure your helper sub classes in separate files.

Example

src/helpers.js src/helpers/ src/helpers/errors.js

File helpers.js

module.exports = {
   errors: require('./helpers/errors')
}

File helpers/errors.js

module.exports = {
    generateInternalError: function(){
        //write some internal error code here
    }
};

Structuring your code like this will keep your root helpers file very organized and create a pattern that is easy to replicate for new subclasses.

If you prefer a less modular approach you could simply just return one big JSON object as other's have demonstrated...

module.exports = {
    errors: {
        generateInternalError: function(){
            //internal error code
        },
        generateDatabaseError: function(){
            //db error code
        }
    }
}

Upvotes: 0

user5224313
user5224313

Reputation:

The module.exports property of a file is simply an object that maps names to functions. You can define it arbitrarily, for example:

module.exports = {
    errors: {
        generateInternalError,
        ...
    },
    ...
};

Then, require('./helper').errors.generateInternalError will be defined.

Upvotes: 4

Related Questions