What are Node.js Global Objects ?
Node.js global objects are global in nature and they are available in all modules. We do not need to include these objects in our application, rather we can use them directly.
These objects are modules, functions, strings and object itself are explained as:
1) __filename
The __filename represents the filename of the code being executed. This is the resolved absolute path of this code file. For a main program, this is not necessarily the same filename used in the command line. The value inside a module is the path to that module file.
Example:
Create a js file and name it home.js with the following code:
// Let's try to print the value of __filename console.log( __filename );
Now run the home.js to see the result:
$ node home.js
Based on the location of your program, it will print the main file name as follows:
/project/example/node/home.js
2) __dirname
The __dirname represents the name of the directory that the currently executing script resides in.
Example:
Create a js file name it home.js with the following code:
// Let's try to print the value of __dirname console.log( __dirname );
Now run the home.js to see the result:
$ node home.js
Based on the location of your program, it will print current directory name as follows:
/project/example/node
3) setTimeout(cb, ms)
The setTimeout(cb, ms) global function is used to run callback cb after at least ms milliseconds. The actual delay depends on external factors like OS timer granularity and system load. A timer cannot span more than 24.8 days.
This function returns an opaque value that represents the timer which can be used to clear the timer.
Example:
Create a js file and name it home.js with the following code:
function printHello() { console.log( "Hello, World!!!"); } // Now call above function after 3 seconds setTimeout(printHello, 3000);
Now run the home.js to see the result:
$ node home.js
Verify the output is printed after a little delay.
Hello, World!!!
4) clearTimeout(t)
The clearTimeout(t) global function is used to stop a timer that was previously created with setTimeout(). Here t is the timer returned by the setTimeout() function.
Example:
Create a js file and name it as home.js with the following code:
function printHello() { console.log( "Hello, World!!!"); } // Now call above function after 3 seconds var t = setTimeout(printHello, 3000); // Now clear the timer clearTimeout(t);
Now run the home.js to see the result:
$ node home.js
Verify the output where you will not find anything printed.
5) setInterval(cb, ms)
The setInterval(cb, ms) global function is used to run callback cb repeatedly after at least ms milliseconds. The actual delay depends on external factors like OS timer granularity and system load. A timer cannot span more than 24.8 days.
This function returns an opaque value that represents the timer which can be used to clear the timer using the function clearInterval(t).
Example:
Create a js file and name it as home.js with the following code:
function printHello() { console.log( "Hello, World!!!"); } // Now call above function after 3 seconds setInterval(printHello, 3000);
Now run the home.js to see the result:
$ node home.js
The above program will execute printHello() after every 3 second. Due to system limitation.
Global Objects
There are few other objects which we frequently use in our applications. They are:
1. Console
Used to print information on stdout and stderr.
2. Process
Used to get information on current process. Provides multiple events related to process activities.
Node.js Console
Node.js console is a global object and is used to print different levels of messages to stdout and stderr. There are built-in methods to be used for printing informational, warning, and error messages.
It is used in synchronous way when the destination is a file or a terminal and in asynchronous way when the destination is a pipe.
Console Methods
Following are the list of methods available with the console global object.
1) console.log([data][, ...]): Prints to stdout with newline. This function can take multiple arguments in a printf()-like way.
2) console.info([data][, ...]): Prints to stdout with newline. This function can take multiple arguments in a printf()-like way.
3) console.error([data][, ...]): Prints to stderr with newline. This function can take multiple arguments in a printf()-like way.
4) console.warn([data][, ...]): Prints to stderr with newline. This function can take multiple arguments in a printf()-like way
5) console.dir(obj[, options]): Uses util.inspect on obj and prints resulting string to stdout.
6) console.time(label): Mark a time.
7) console.timeEnd(label): Finish timer, record output.
8) console.trace(message[, ...]): Print to stderr 'Trace :', followed by the formatted message and stack trace to the current position.
Node.js Process
The process object is a global object and can be accessed from anywhere. There are several methods available in a process object.
Process Events
The process object is an instance of EventEmitter and emits the following events:
1) exit: Emitted when the process is about to exit. There is no way to prevent the exiting of the event loop at this point, and once all exit listeners have finished running, the process will exit.
2)beforeExit: This event is emitted when node empties its event loop and has nothing else to schedule. Normally, the node exits when there is no work scheduled, but a listener for 'beforeExit' can make asynchronous calls, and cause the node to continue.
3) uncaughtException: Emitted when an exception bubbles all the way back to the event loop. If a listener is added for this exception, the default action (which is to print a stack trace and exit) will not occur.
4) Signal Events: Emitted when the processes receives a signal such as SIGINT, SIGHUP, etc.
If you have any queries regarding this blog or need any help you can contact us on: contact@codersarts.com
Node.js, Node.js Global Objects, Node.js Console, Node.js Assignment, Node.js Assignment Help, Node.js Project, Node.js Process
Comments