Node JS Interview Questions and Answers Set 3

21.How To Update A Dependency Using Npm?

Update package.json and change the version of the dependency which to be updated and run the following command.

C:Nodejs_WorkSpace>npm update

22.What Is Purpose Of Buffer Class In Node?

Buffer class is a global class and can be accessed in application without importing buffer module. A Buffer is a kind of an array of integers and corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.

23.Which Module Is Used For File Based Operations?

fs module is used for file based operations.

var fs = require(“fs”)

24.How Many Types Of Streams Are Present In Node?

In Node.js, there are four types of streams.

Readable – Stream which is used for read operation.
Writable – Stream which is used for write operation.
Duplex – Stream which can be used for both read and write operation.
Transform – A type of duplex stream where the output is computed based on input.

25.How Will You Read A Directory?

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

fs.readdir(path, callback)


Here is the description of the parameters used:

path – This is the directory name including path.

callback – This is the callback function which gets two arguments (err, files) where files is an array of the names of the files in the directory excluding ‘.’ and ‘..’.

Weekend / Weekday Batch

26.What Is Difference Between Synchronous And Asynchronous Method Of Fs Module?

Every method in fs module have synchronous as well as asynchronous form. Asynchronous methods takes a last parameter as completion function callback and first parameter of the callback function is error. It is preferred to use asynchronous method instead of synchronous method as former never block the program execution where the latter one does.

27.What Is The Purpose Of __filename Variable?

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.

28.Why to use “SetTimeout” in Node.JS?

This is the global function and it is used to run the callback after some milliseconds.

Syntax of this method –

setTimeout(callbackmethod, millisecs)

29.List out the layers involved in Web App Architechure?

Below are the layers used in Web Apps –

  • Client – Which makes HTTP request to the server. Eg: Browsers.
  • Server – This layer is used to intercept the requests from client.
  • Business – It will have application server utilized by web servers for processing.
  • Data – This layer will have databases mainly or any source of data.

3o.Why to use Net.socket in Node.JS?

This object is an abstraction of a local socket or TCP. net.Socket instances implement a duplex Stream interface. These can be created by the user and used as a client (with connect() function) or they can be created by Node and can be passed to the user through the ‘connection’ event of a server.