21.What is Event Loop?
Node js is a single threaded application but it support concurrency via concept of event and callbacks. As every API of Node js are asynchronous and being a single thread, it uses async function calls to maintain the concurrency. Node uses observer pattern. Node thread keeps an event loop and whenever any task get completed, it fires the corresponding event which signals the event listener function to get executed.
22.What is Event Emmitter?
EventEmitter class lies in events module. It is accessibly via following syntax:
//import events module
var events = require(‘events’);
//create an eventEmitter object
var eventEmitter = new events.EventEmitter();
When an EventEmitter instance faces any error, it emits an ‘error’ event. When new listener is added, ‘newListener’ event is fired and when a listener is removed, ‘removeListener’ event is fired.
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.
23.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.
24.What is Piping in Node?
Piping is a mechanism to connect output of one stream to another stream. It is normally used to get data from one stream and to pass output of that stream to another stream. There is no limit on piping operations. Consider the above example, where we’ve read test.txt using readerStream and write test1.txt using writerStream. Now we’ll use the piping to simplify our operation or reading from one file and writing to another file.
25.Which module is used for file based operations?
fs module is used for file based operations.
var fs = require(“fs”)
26.Which module is used for buffer based operations?
buffer module is used for buffer based operations.
var buffer = require(“buffer”)
27.Which module is used for web based operations?
http module is used for web based operations.
var http = require(“http”)
28.fs module provides both synchronous as well as asynchronous methods.
29.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.
30.Name some of the flags used in read/write operation on files.
flags for read/write operations are following:
r – Open file for reading. An exception occurs if the file does not exist.
r+ – Open file for reading and writing. An exception occurs if the file does not exist.
rs – Open file for reading in synchronous mode. Instructs the operating system to bypass the local file system cache. This is primarily useful for opening files on NFS mounts as it allows you to skip the potentially stale local cache. It has a very real impact on I/O performance so don’t use this flag unless you need it. Note that this doesn’t turn fs.open() into a synchronous blocking call. If that’s what you want then you should be using fs.openSync()
rs+ – Open file for reading and writing, telling the OS to open it synchronously. See notes for ‘rs’ about using this with caution.
w – Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
wx – Like ‘w’ but fails if path exists.
w+ – Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
wx+ – Like ‘w+’ but fails if path exists.
a – Open file for appending. The file is created if it does not exist.
ax – Like ‘a’ but fails if path exists.
a+ – Open file for reading and appending. The file is created if it does not exist.
ax+’ – Like ‘a+’ but fails if path exists.
What are streams?