Let us discuss some of the export patterns used in node. Common export patterns observed are:
- Exports a Namespace
- Exports a Function
- Exports a Constructor
Exports a Namespace
Module.exports is used to define the end point for accessing the module functionality. In exports a namespace, the module returns object consists of properties and functions. Module invoking the dependent module can access the properties from the returned object and invoke the associated functions.
For example, consider the following dependency module defining the circle properties and functions. Circle module define a property to hold the value of pi and two functions for calculating the area and circumference of the circle.
circle.js
module.exports={
pi: 3.14,
area: function (r){
return this.pi * r * r;
},
circumference: function (r){
return 2 * this.pi * r;
}
};
app.js
var circle = require(“./circle”);
console.log(circle);
console.log(circle.pi);
console.log(circle.area(5));
console.log(circle.circumference(5));
Main module receives an object of the circle module along with the specified properties and functions.
Refer the following link for more about node and related technologies
https://leanpub.com/webappwithnode