Node JS offers several Inbuilt core objects. Here we gonna discuss about one of the important Core Object - Process.
The process object is a global object and can be accessed from anywhere. Here I have listed out few Methods and properties of an Object.
Process Events
Event: '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. Therefore you must only perform synchronous operations in this handler.
process.on('exit', function(code) {
// Sync function only
console.log('About to exit with code:', code);
});
console.log('About to exit with code:', code);
});
Event: 'beforeExit'
This event is emitted when node empties it's event loop and has nothing else to schedule. Normally, node exits when there is no work scheduled, but a listener for 'beforeExit' can make asynchronous calls, and cause node to continue.
'beforeExit' is not emitted for conditions causing explicit termination, such as process.exit() or uncaught exceptions, and should not be used as an alternative to the 'exit' event unless the intention is to schedule more work.
Event: 'uncaughtException'
Emitted when an exception bubbles all the way back to the event loop. If a listener is added for this exception
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
setTimeout(function() {
console.log('This will still run.');
},500);
// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');
console.log('Caught exception: ' + err);
});
setTimeout(function() {
console.log('This will still run.');
},500);
// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');
process.argv
An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.
process.execPath
This is the absolute pathname of the executable that started the process.
C:\Program Files\nodejs\node.exe
process.cwd()
Returns the current working directory of the process.
console.log('Current directory: ' + process.cwd());
process.chdir(directory)
Changes the current working directory of the process or throws an exception if that fails.
console.log('Starting directory: ' + process.cwd());
try {
process.chdir('/tmp');
console.log('New directory: ' + process.cwd());
}
catch (err) {
console.log('chdir: ' + err);
try {
process.chdir('/tmp');
console.log('New directory: ' + process.cwd());
}
catch (err) {
console.log('chdir: ' + err);
}
process.env
An object containing the user environment.
process.exit([code])
Ends the process with the specified code. If omitted, exit uses the 'success' code 0.
To exit with a 'failure' code:
process.exit(1);
The shell that executed node should see the exit code as 1.
process.exitCode
A number which will be the process exit code, when the process either exits gracefully, or is exited via process.exit() without specifying a code.
Specifying a code to process.exit(code) will override any previous setting of process.exitCode.
process.version
A compiled-in property that exposes NODE_VERSION.
console.log('Version: ' + process.version);
process.versions
A property exposing version strings of node and its dependencies.
console.log(process.versions);
Will print something like:
{ http_parser: '1.0',
node: '0.10.4',
v8: '3.14.5.8',
ares: '1.9.0-DEV',
uv: '0.10.3',
zlib: '1.2.3',
modules: '11',
openssl: '1.0.1e' }
node: '0.10.4',
v8: '3.14.5.8',
ares: '1.9.0-DEV',
uv: '0.10.3',
zlib: '1.2.3',
modules: '11',
openssl: '1.0.1e' }
process.config
An Object containing the JavaScript representation of the configure options that were used to compile the current node executable.
process.memoryUsage()
Returns an object describing the memory usage of the Node process measured in bytes.
process.nextTick(callback)
Once the current event loop turn runs to completion, call the callback function.
console.log('start');
process.nextTick(function() {
console.log('nextTick callback');
});
console.log('scheduled');
process.nextTick(function() {
console.log('nextTick callback');
});
console.log('scheduled');
process.uptime()
Number of seconds Node has been running.
To know more details about all the topics in Node JS, Please visit Best Node JS Training in Chennai.
I am really enjoying reading your well written articles. I think you spend numerous effort and time updating your blog. AWS Training in chennai
ReplyDelete