Node.js

Node.js: Modules and Buffers

This article is part of our Academy Course titled Building web apps with Node.js.

In this course, you will get introduced to Node.js. You will learn how to install, configure and run the server and how to load various modules.

Additionally, you will build a sample application from scratch and also get your hands dirty with Node.js command line programming.
Check it out here!

 
 
 
 

1. Introduction

To build structural applications adopting different patterns in different files, node.js provides us with the option for a module based system.

In node.js, we add variables or functions to the object ‘module.exports’. By invoking the ‘require’ function in a script which uses the module, will return the corresponding object (variables, functions etc …)

  • For a module: module.exports points to an object.
  • For a script: require(“module-filename”) returns that object.

There are two types of node.js for the module interaction:

1. When node.js does not have a relative hint about the file location.

Example:

var http = require('http');

Here node.js looks for the core module named http and returns that module. This module should have been stored in the global repository space of our node.js application.

Now, for the non-core modules, node.js will look into the folder structure and try to find out a folder name called node_modules and then find the module reference from that module. In other words, it will look for a required file named as per the required file reference with ajavascript extension (.js).

2. Now when node.js works with a single file in some folder.

Example:

var http = require('./filename');

Here node.js has the option to search for both filename.js and index.js – where the actual javascript file is referenced.

2. Load and Export Modules

In the node.js platform, there are requirements to have utility functions and common functions. But by default, all variables, functions, classes and member function of classes are accesible within a particular file only.

Thus, some function declared within one file can not be accessed from another js file. As per definition, related node.js functionalities are described as modules. By default, when we write javascript functions within modules, they are accessed within that class and they are private to that module only, i.e. no other module or file can access these elements (variables, functions etc.). However, there are ways to expose those functions, variables or classes as needed.

2.1. Load

In node.js, different external resources can be loaded in another module with the following syntax:

var checkData = require('./validatorutil');

Here, the ‘require’ keyword is used to load an external module in the working module and the return type is assigned to a variable.

2.2. Export

To expose functions and classes, we use module.exports and export those as per our requirements.

In the function below, we have used the validator library of node.js.

Check valid Email Address:

var checkEmail = function(value) {
   try {
      check(value).isEmail();
   } catch (e) {
      return e.message; //Invalid integer
   }
   return value;
};

module.exports.checkEmail = checkEmail;

To use this in our required file we have written them as:

var checkData = require('./validation');

console.log("In testvalidation -->"+checkData.checkEmail("piyas.de@gmail.com"));

console.log("In testvalidation -->"+checkData.checkEmail("piyas.de@gmailcom")); // Return error

Some other useful functions that we can use for our daily tasks are:

// Chcek minimum length

var checkMinLength = function(value,len) {

   try {
       check(value,'Please specify a minimum length of %1').len(len);
   } catch (e) {
       return e.message;
   }
   return value;

};

//Check Maximum length

var checkMaxLength = function(value,lenmax) {
   try {
       check(value,'Please specify a maximum length of %2').len(0,lenmax);
   } catch (e) {
       return e.message;
   }
   return value;
};

//Chcek boundary length

var checkBoundaryLength = function(value,lenmin, lenmax) {
    try {
         check(value,'The message needs to be between %1 and %2 characters long (you passed "%0")').len(lenmin,lenmax);

    } catch (e) {
         return e.message;
    }
    return value;
};

//Check Numeric

var checkNumeric = function(value) {
   try {
      check(value).isNumeric();
   } catch (e) {
      return e.message; 
   }
   return value;
};

//Check AlphaNumeric

var checkAlphaNumeric = function(value) {
   try {
      check(value).isAlphanumeric();
   } catch (e) {
      return e.message; 
   }
   return value;
};

//Check LowerCase

var checkLowerCase = function(value) {
   try {
      check(value).isLowercase();
   } catch (e) {
      return e.message; 
   }
   return value;
};

//Check UpperCase

var checkUpperCase = function(value) {
   try {
      check(value).isUppercase();
   } catch (e) {
      return e.message; 
   }
   return value;
};

module.exports.checkMinLength = checkMinLength;
module.exports.checkMaxLength = checkMaxLength;
module.exports.checkBoundaryLength = checkBoundaryLength;
module.exports.checkNumeric = checkNumeric;

module.exports.checkAlphaNumeric = checkAlphaNumeric;
module.exports.checkLowerCase = checkLowerCase;
module.exports.checkUpperCase = checkUpperCase;

And their use in the working file, respectively:

console.log("In testvalidation -->"+checkData.checkMinLength("abc",2));
console.log("In testvalidation -->"+checkData.checkMinLength("abc",4)); // Return error
console.log("In testvalidation -->"+checkData.checkMaxLength("abc",2)); // Return error
console.log("In testvalidation -->"+checkData.checkMaxLength("abc",4));
console.log("In testvalidation -->"+checkData.checkBoundaryLength("abc",2,4));
console.log("In testvalidation -->"+checkData.checkBoundaryLength("abc",4,6)); // Return error
console.log("In testvalidation -->"+checkData.checkNumeric("12"));
console.log("In testvalidation -->"+checkData.checkNumeric("ABC")); // Return error
console.log("In testvalidation -->"+checkData.checkAlphaNumeric("_!")); // Return error
console.log("In testvalidation -->"+checkData.checkAlphaNumeric("A23"));
console.log("In testvalidation -->"+checkData.checkLowerCase("lower"));
console.log("In testvalidation -->"+checkData.checkLowerCase("Lower")); // Return error
console.log("In testvalidation -->"+checkData.checkUpperCase("UPPER")); 
console.log("In testvalidation -->"+checkData.checkUpperCase("upPeR")); // Return error

This is just the starting point to understand the load and export functionality in node.js.

You may download the associated Source code to mess around with.

3. Node.js Buffer Operations

Pure javascript is not efficient in handling binary data. For this reason, Node.js has a native layer implementation for handling binary data, which is a buffer implementation with the syntax of javascript. In node.js, we utilize buffers in all operations that read and write data. Thus, Node actually provides an interface for handling binary data in a quite efficient way.

Some points to be noted for the node.js buffer implementation:

  1. A Buffer can not be resized.
  2. Raw data from the transport layer are stored in buffer instances.
  3. A Buffer corresponds to raw memory allocation outside the V8 javascript engine.

Now let’s examine the syntax:

  • To create a buffer for utf-8 encoded string by default:
    var buf = new Buffer('Hello Node.js...');
    
  • To print the buffer, we can write:
    console.log(buf);
    
  • To print the text,as we have entered, we have to write:
    console.log(buf.toString());
    
  • To create a buffer of pre-allocated size, we write:
    var buf = new Buffer(256);
    
  • Also we can store a value in each of the pre-allocated arrays with the following syntax:
    buf[10] = 108;
    
  • We can assign encoding while creating the buffer or while printing it by using:
    var buf = new Buffer('Hello Node.js...','base64');
    

    or

    console.log(buf.toString('base64'));
    
  • A buffer can be sliced to small buffers with the following syntax:
    var buffer = new Buffer('this is a good place to start');
    
    var slice = buffer.slice(10, 20);
    

    Here the new buffer variable slice will be populated with “good place” which starts from 10th byte and ends with 20th byte of the old buffer.

  • Also to copy from a buffer to another buffer variable we write:
    var buffer = new Buffer('this is a good place to start');
    
    var slice = new Buffer(10);
    
    var startTarget = 0,start = 10,end = 20;
    
    buffer.copy(slice, startTarget, start, end);
    

    Here the values will be copied from old buffer to new buffer.

  • To copy the whole buffer with any value, we can use the ‘fill’ method:
    var buffer = new Buffer(50);
    
    buffer.fill("n");
    
  • buf.toJSON() returns a json representation of the Buffer object, identical to a json array.
    var buf = new Buffer('test');
    
    var json = JSON.stringify(buf);
    
    console.log(json);
    

    Here json.stringify is called implicitly to maintain the json representation.

There is in-depth documentation for the Buffer class which can be found here.

Buffer allows node.js developers to access data as it is in its internal representation (as per its memory allocation) and returns the number of bytes used in the particular case. On the contrary, String takes the encoding and returns the number of characters used in the data.

While working with binary data, developers frequently need to access data that have no encoding – like the data in image files. Examples also include, reading an image file from a TCP connection or reading a file from the local disk etc.

4. Event Emitter in Node.js

The Javascript classes which inherit from Event Emitter in Node.js, generally are a source of different event/events collection in our apps. With those classes we can listen in the .on() function on the object for the event that was specified in the arguments and work accordingly with the callback function codes.

Example:

var str = '';

someObject.on('dataReceived', function(data) {
    dataReceived += data;
  })
 .on('end', function() {
    console.log('The data received: ' + data);
  })

The on() function returns a reference to the object it is attached to and can chain n number of event listeners.

Now if we have to emit events from a function we need to write the following code:

var util = require('util');

In order to have inheritance functionality with EventEmitter in Javascript, we have to inherite the particular class with:

util.inherits(myEventEmitterClass, EventEmitter); 

Here the myEventEmitterClass will be the class which will inherit the EventEmitter functionality.

And then the actual Event emiiting will happen in the following code:

myEventEmitterClass.prototype.emittedMethod = function() {
	console.log('before the emittedMethod');
	this.emit('emittedevent');
	console.log('after the emittedMethod');
}

So, while the myEventEmitterClass will be used in the system, the emittedEvent will be exposed like:

someObject.on('emittedevent', function() {
    console.log('The emittedevent is called');
})

The above is a short introduction of the EventEmitter in node.js. Utilizing this functionality can be a little bit tricky to a new node.js programmer, but will remain useful in various application areas.

5. Download the Source Code

This was a tutorial of Buffers and Modules in Node.js. You may download the source code of this tutorial: Node_TestValidation.zip

Piyas De

Piyas is Sun Microsystems certified Enterprise Architect with 10+ years of professional IT experience in various areas such as Architecture Definition, Define Enterprise Application, Client-server/e-business solutions.Currently he is engaged in providing solutions for digital asset management in media companies.He is also founder and main author of "Technical Blogs (Blog about small technical Know hows)"
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
pooja mehta
pooja mehta
8 years ago

Hi,

Can you help me with this error?

module.js:340
throw err;
^

Error: Cannot find module ‘validator’
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:289:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)

I have run npm install validator

please help

regards,
Poja

Abel
Abel
7 years ago
Reply to  pooja mehta

I had same problem. I had to install validator.js:

MacBookPro2k:Node_TestValidation$ npm install validator
validator@7.0.0 ../../node_modules/validator

After this, I had other more problem:
[…]
In testvalidation –>check is not a function
[…]

I solved with this:

Old code –> var check = require(‘validator’).check;

Replace to –> var check = require(‘validator’);

After that, I re-code the example:

var checkEmail = function(value) {

try {

return check.isEmail(value);

} catch (e) {

return e.message; //Invalid integer

}

};
[…]

Back to top button