The question is about nodejs module pattern code structure.
My goal is to initialise the module at one time and export the module using module.export
so that, it's available in other files.
I took Twilio for explaining.
Here I want to initialize twilo
using credentials and export it as twilioClient
, which I can reuse without reinitialising.
But I also need a dynamic Twilio client, ie, I give the credentials as params and create a client using that param.
That was my requirement and I came across this structure,
const twilio = require('twilio');
function init (config) {
const twilioSID = config.TWILIO_ACCOUNT_SID;
const twilioAuth = config.TWILIO_AUTH_TOKEN;
const client = twilio(twilioSID, twilioAuth);
return client;
}
const twilioSID = process.env.TWILIO_ACCOUNT_SID;
const twilioAuth = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(twilioSID, twilioAuth);
module.exports = {
twilioClient: client,
twilioClientFn: init
};
Is this structure okay? is there any room for improvement? Thank You