Exports Patterns in node.js

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

 

Advertisements

About ambilykk

I am a Technology Evangelist on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Azure and Visual Studio. Technology adoption and learning is my key strength and technology sharing is my passion.
This entry was posted in AngularJs, Book, Node and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s