What is global objects ?
uses of global objects in node.js
__filename
The __filename represents the filename of the code being executed. it return the name of the file.
File Name :
console.log( __filename );
__dirname
The __dirname represents the name of the directory that the currently executing script resides in.
File Name :
console.log( __dirname );
setTimeout(cb, ms)
The setTimeout(cb, ms) global function is used to run callback cb after at least ms milliseconds.
File Name :
function say_hello() {
console.log( "Hello, Ittutorial!");
}
// Now call above function after 5 seconds
setTimeout(say_hello, 5000);
clearTimeout(t)
The clearTimeout(t) global function is used to stop a timer that was previously created with setTimeout().
File Name :
function say_hello() {
console.log( "Hello, Sana!");
}
// Now call above function after 2 seconds
var obj = setTimeout(say_hello, 2000);
// Now clear the timer
clearTimeout(obj);
setInterval(cb, ms)
The setInterval(cb, ms) global function is used to run callback cb repeatedly after at least ms milliseconds.
File Name :
function say_hello() {
console.log( "Hello, Sana!");
}
// Now call above function after 2 seconds
setInterval(say_hello, 2000);
Console
Node.js console is a global object and is used to print different levels of messages to stdout and stderr.
File Name :
console.log([data][, ...])
console.info([data][, ...])
console.error([data][, ...])
console.warn([data][, ...])
console.dir(obj[, options])
console.time(label)
console.timeEnd(label)
console.trace(message[, ...])
console.info("Program Started");
var counter = 10;
console.log("Counter: %d", counter);
console.time("Getting data");
//
// Do some processing here...
//
console.timeEnd('Getting data');
console.info("Program Ended")
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
File Name :
exit
beforeExit
uncaughtException
Signal Events
process.on('exit', function(code) {
// Following code will never execute.
setTimeout(function() {
console.log("This will not run");
}, 0);
console.log('About to exit with code:', code);
});
console.log("Program Ended");
File Name :
// Printing to console
process.stdout.write("Hello World!" + "\n");
// Reading passed parameter
process.argv.forEach(function(val, index, array) {
console.log(index + ': ' + val);
});
// Getting executable path
console.log(process.execPath);
// Platform Information
console.log(process.platform);
Methods :-
File Name :
abort()
chdir(directory)
cwd()
exit([code])
getgid()
setgid(id)
getuid()
setuid(id)
getgroups()
setgroups(groups)
initgroups(user, extra_group)
kill(pid[, signal])
memoryUsage()
nextTick(callback)
umask([mask])
uptime()
hrtime()
File Name :
// Print the current directory
console.log('Current directory: ' + process.cwd());
// Print the process version
console.log('Current version: ' + process.version);
// Print the memory usage
console.log(process.memoryUsage());
Previous
Next