Node JS Interview Questions and Answers Set 7

61.What is the role of assert in Node.js?

The Node.js Assert is a way to write tests. It provides no feedback when running your test unless one fails. The assert module provides a simple set of assertion tests that can be used to test invariants. The module is intended for internal use by Node.js, but can be used in application code via require (‘assert’). For example:

var assert = require(‘assert’);

function add (a, b) {

return a + b;

}

var expected = add(1,2);

assert( expected === 3, ‘one plus two is three’);

62. Does Node.js supports cryptography?

Yes, Node.js Crypto module supports cryptography. It provides cryptographic functionality that includes a set of wrappers for open SSL’s hash HMAC, cipher, decipher, sign and verify functions. For example:

const crypto = require(‘crypto’);

const secret = ‘abcdefg’;

const hash = crypto.createHmac(‘sha256’, secret)

.update(‘Welcome to JavaTpoint’)

.digest(‘hex’);

console.log(hash);

63. What is error-first callback?

Error-first callbacks are used to pass errors and data. If something goes wrong, the programmer has to check the first argument because it is always an error argument. Additional arguments are used to pass data.

fs.readFile(filePath, function(err, data) {

if (err) {

//handle the error

}

// use the data object

});

64. What is the purpose of Node.js?

These are the following purposes of Node.js:

  • Real-time web applications
  • Network applications
  • Distributed systems
  • General purpose applications

65. Explain “NewListener” in Node.JS?

This event is being emitted whenever any listener is added. So when event is triggered the listener may not have been removed from listener array for the event.

NODE JS & CERTIFICATION
Weekend / Weekday Batch

66.What Is The Purpose Of Settimeout Function?

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.

67.How Will You Delete A Directory?

Following is the syntax of the method to remove a directory:

fs.rmdir(path, callback)

Parameters

Here is the description of the parameters used:

path – This is the directory name including path.

callback – This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

68. Name Some Of The Events Fired By Streams.

Each type of Stream is an EventEmitter instance and throws several events at different instance of times. For example, some of the commonly used events are:

data – This event is fired when there is data is available to read.

end – This event is fired when there is no more data to read.

error – This event is fired when there is any error receiving or writing data.

finish – This event is fired when all data has been flushed to underlying system

69. How Will You Get Information About A File Using Node?

Following is the syntax of the method to get the information about a file:

fs.stat(path, callback)

Parameters

Here is the description of the parameters used:

path – This is string having file name including path.

callback – This is the callback function which gets two arguments (err, stats) where stats is an object of fs.Stats type which is printed below in the example.

70. What Is The Purpose Of __dirname Variable?

The __dirname represents the name of the directory that the currently executing script resides in.