Google News
logo
What are Globals in Node.js ?


There are three keywords in Node.js which constitute as Globals. These are Global, Process, and Buffer.

Global.

The Global keyword represents the global namespace object. It acts as a container for all other <global> objects. If we type <console.log(global)>, it’ll print out all of them.
 
An important point to note about the global objects is that not all of them are in the global scope, some of them fall in the module scope. So, it’s wise to declare them without using the var keyword or add them to Global object.
 
Variables declared using the var keyword become local to the module whereas those declared without it get subscribed to the global object.

Process.

It is also one of the global objects but includes additional functionality to turn a synchronous function into an async callback. There is no boundation to access it from anywhere in the code. It is the instance of the EventEmitter class. And each node application object is an instance of the Process object.
 
It primarily gives back the information about the application or the environment.
 
<process.execPath> – to get the execution path of the Node app.
<process.Version> – to get the Node version currently running.
<process.platform> – to get the server platform.
 
Some of the other useful Process methods are as follows.
 
<process.memoryUsage> – To know the memory used by Node application.
<process.NextTick> – To attach a callback function that will get called during the next loop. It can cause a delay in executing a function.

Buffer.

The Buffer is a class in Node.js to handle binary data. It is similar to a list of integers but stores as a raw memory outside the V8 heap.
 
We can convert JavaScript string objects into Buffers. But it requires mentioning the encoding type explicitly.
 
<ascii> – Specifies 7-bit ASCII data.
<utf8> – Represents multibyte encoded Unicode char set.
<utf16le> – Indicates 2 or 4 bytes, little endian encoded Unicode chars.
<base64> – Used for Base64 string encoding.
<hex> – Encodes each byte as two hexadecimal chars.

Here is the syntax to use the Buffer class.
var buffer = new Buffer(string, [encoding]);
The above command will allocate a new buffer holding the string with <utf8>  as the default encoding. However, if you like to write a <string>  to an existing buffer object, then use the following line of code.
 
buffer.write(string)

This class also offers other methods like <readInt8> and <writeUInt8> that allows read/write from various types of data to the buffer.
LATEST ARTICLES