a blog for those who code

Thursday 3 July 2014

Globals in Nodejs

There are many objects available to all node applications which we require to use in our simple or complex node program. These are Global, Process and Buffer which are combinedly termed as Globals. So let us see the definition of Global, Process and Buffer :


Global : Its a global namespace object
Process : Its also a global object but it provides essential functionality to transform a synchronous function into a asynchronous callback.
Buffer : Raw data is stored in instances of the Buffer class.

Global

Global is the global namespace object. When we type 

>console.log(global)

it prints out all the other global objects. One important point with Global object is that these objects aren't actually in the global scope but they are in the module scope. So the common advice is to either "declare the variable without the var keyword" or "add the variable to the global object". 

A variable declared with or without the var keyword got attached to the global object but with a slight variation. Variables declared with the var keyword remain local to a module whereas those declared without it get attached to the global object.

For example 

> var value = "Test";
> console.log(global);

Using this we can see our variable as a new property of global at the bottom.

Now,

> value1 = global;
> console.log(global);

The global object interface is printed out to the console and at the bottom we can see the local variable assigned as "Circular Reference". 

Process

Its also a global object but it provides essential functionality to transform a synchronous function into a asynchronous callback. It can be accessed from anywhere and also its an instance of EventEmitter. Each node application is an instance of a Node Process object. 

It mainly provides identification or information about the application and its environment. Such as

process.execPath : returns the execution path for the Node application
process.Version : returns the Node version
process.platform : identifies the server platform.

The process object also has STDIO (Standard IO) streams such as stdin, stdout and stderr. One more useful Process method is memoryUsage, which provide us with the information of how much memory Node application is using.

process.NextTick method attaches a callback function that's fired during the next loop in the Node event loop. process.NextTick method can be used to delay a function but asynchronously. So for whatever reason if you want to delay a fucntion asynchronously, you can use process.nextTick. It can also be used when you are doing some complex operations which is time consuming then you can divide that complex function into sections and each sections can be called using process.nextTick thus allowing other requests in the the Node application to be processed without waiting for the time consuming process to finish.

Buffer

The Buffer class is a way of handling binary data in Node.A buffer is similar to an arrays of integers but corresponds to a raw memory allocation outside the V8 heap. 

Converting between Buffers and JavaScript string objects requires an explicit encoding method which are :-

ascii - for 7 bit ASCII data only.
utf8 - multibyte encoded Unicode characters
utf16le - 2 or 4 bytes, liitle endian encoded Unicode characters.
base64 - Base64 string encoding
hex - Encode each byte as two hexadecimal characters.

> var b = new Buffer(str, [encoding]);

It allocates a new buffer containing the given str, encoding defaults to 'utf8'. We can also write a string to an existing buffer.

> b.write(str)

There are several methods for reading and writing various types of data to the buffer, they are buffer.readInt8 and buffer.writeUInt8.

Please Like and Share the Blog, if you find it interesting and helpful.


Related Articles

No comments:

Post a Comment