1/22/2017 - 2:43 PM

exporting modules

exporting modules

//As developers, we often face situations where we need to use unfamiliar code. A question //will arise during these moments. How much time should I invest in understanding the code //that I’m about to use? A typical answer is learn enough to start coding; then explore that //topic further when time permits. Well, the time has come to gain a better understanding of //module.exports and exports in Node.js. Here’s what I have learned.

//A module encapsulates related code into a single unit of code. When creating a module, //this can be interpreted as moving all related functions into a file. Let’s illustrate this //point with an example involving an application built with Node.js. Imagine that we created //a file called greetings.js and it contains the following two functions:

// exporting by calling exports 

// greetings.js
// var exports = module.exports = {};
exports.sayHelloInEnglish = function() {
  return "HELLO";
exports.sayHelloInSpanish = function() {
  return "Hola";