Node JS Interview Questions and Answers Set 4

31.Explain “DNS module” in Node.JS?

This module is used for DNS lookup and to use underlying OS name resolution. This used to provide asynchronous network wrapper. DNS module can be imported like –

var mydns = require(“dns”)

32.How to truncate the file in Node.JS?

Below command can be used for truncating the file –

fs.ftruncate(fd, len, callback)

33.Explain “Event Emitter” in Node.JS?

It is a part of Events module. When instance of EventEmitter faces any error, it will emit an ‘error’ event. “Event Emitters” provides multiple properties like – “emit” and “on”.

  • “on” property is used for binding the function with event.
  • “emit” property is used for firing an event.

34.Why to use exec method for Child process module?

“exec” method runs a command in a shell and buffers the output. Below is the command –

child_process.exec(command[, options], callback)

35.Explain Piping Stream?

This is a mechanism of connecting one stream to other and this is basically used for getting the data from one stream and pass the output of this to other stream.

NODE JS & CERTIFICATION
Weekend / Weekday Batch

36.What is the use of method – “fork()”?

This method is a special case for method- “spawn()” for creating node processes. The method signature –

child_process.fork(modulePath[, args][, options])

37.Explain FS module ?

Here FS stands for “File System” and fs module is used for File I/O. FS module can be imported in the following way –

var test = require(“fs”)

38. How you can update NPM to new version in Node.js?

Below commands can be used for updating NPM to new version –

$ sudo npm install npm -g

/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js

npm@2.7.1 /usr/lib/node_modules/npm

39.Explain callback in Node.js?

Callback is called once the asynchronous operation has been completed. Node.js heavily uses callbacks and all API’s of Node.js are written to support callbacks.

40.Explain exit codes in Node.JS? List out some exit codes?

Exit code will be used when the process needs to be ended with specified code. Below are the list of exit codes in Node.JS –

  • Fatal Error
  • Non-function Internal Exception Handler
  • Internal JavaScript Parse Error
  • Uncaught Fatal Exception
  • Unused
  • Internal JavaScript Evaluation Failure
  • Internal Exception Handler Run-Time Failure