Saturday 3 June 2017

Nodejs - Global or local initialisation of libraries

I am a bit confused as to which of the below is the better practice and why? In the first, we initialise the library globally and in the second case, its done inside a function.

Code 1

var twilio_client = require('twilio')(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);

function_sendSMS(from, to){
   twilio_client.messages.create({
     from: from,
     to: to,
     body: "You just sent an SMS from Node.js using Twilio!"
     }, function(err, message) {
     if(err) {
      console.error(err.message);
     }
  });
}

Code 2

function_sendSMS(from, to){

   var twilio_client = require('twilio')(
     process.env.TWILIO_ACCOUNT_SID,
     process.env.TWILIO_AUTH_TOKEN
   );

   twilio_client.messages.create({
     from: from,
     to: to,
     body: "You just sent an SMS from Node.js using Twilio!"
     }, function(err, message) {
     if(err) {
      console.error(err.message);
     }
  });
}



via npm install mad-monkey

No comments:

Post a Comment