Node.js Interview Questions Part - 3

12.  What is the package.json file in Node.js project?


All npm packages contain a file in the project root, called package.json. This file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies.  
Node itself is only aware of two fields in the package.json:

  "name" : "barebones",
  "version" : "0.0.0",

13.   What are the buffers and how to create the buffers?


Reading data from secondary storage (Hard disk) is time consuming than reading from main memory. Hence OS brings some data to RAM(main memory) for easier and fast accessing. These chunks of data are called Buffers.

Node.js introduced the Buffer class to deal with binary data because JavaScript has no mechanism for reading or manipulation of the binary data.
Each buffer has some raw memory which is allocated outside the V8.

Creating buffer:

var buffer = new Buffer(16);
buffer.write("Welcome", "utf-8")

Output: 7

14.  What are streams and what are the different types of stream?


Just like arrays or strings, Streams are collections of data. The difference is that streams might not be available all at once, and they don't have to fit in memory. This makes streams really powerful when working with large amounts of data, or data that's coming from an external source one chunk at a time.

A Stream is an abstract interface that is used to work with stream data in Node.js application. It lets you read data from a source or write data to a destination in continuous fashion.

There are four types of stream in Node.js
  • Readable: It is used to read operation.
  • Writeable: It is used to write operation
  • Duplex: Used for both read and write operation.
  • Transform: This is used to compute the output based on input.

15.  What is EventEmitter In Node.Js?


Node.js application is the event-driven application. Node.js uses the event module for event handling.
All objects in Node.js which emit events are the instances of events.EventEmitter.

EventEmitter class lies in the events module.

EventEmitter provides multiple properties like on and emit. "on" property is used to bind a function with the event and "emit" is used to fire an event.

16.  What are the streams piping in Node.js?


Stream piping is the way to connect one output stream to another output stream. Node.js provides the pipe() method for this purpose.

We can pass one readable stream as a parameter in pipe() method and this stream will be write into another stream.


var fs = require('fs');
var rs = fs.createReadStream('input.txt');
var ws = fs.createWriteStream('output.txt');