What are streams?
Answer:
Streams are objects that let you read data from a source or write data to a destination in continous fashion.
ViewName some of the flags used in read/write operation on files.
Answer:
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 is difference between synchronous and asynchronous method of fs module?
Answer:
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.
ViewWhich module is used for web based operations?
Answer:
http module is used for web based operations.
var http = require("http")View
Which module is used for buffer based operations?
Answer:
buffer module is used for buffer based operations.
var buffer = require("buffer")View
Which module is used for file based operations?
Answer:
fs module is used for file based operations.
var fs = require("fs")View
What is Piping in Node?
Answer:
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.
ViewWhat is purpose of Buffer class in Node?
Answer:
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.
ViewWhat is Event Emmitter?
Answer:
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.
ViewHow Node prevents blocking code?
Answer:
By providing callback function. Callback function gets called whenever corresponding event triggered.
ViewWhat is a blocking code?
Answer:
If application has to wait for some I/O operation in order to complete its execution any further then the code responsible for waiting is known as blocking code.
ViewWhat is Callback?
Answer:
Callback is an asynchronous equivalent for a function. A callback function is called at the completion of a given task. Node makes heavy use of callbacks. All APIs of Node are written is such a way that they supports callbacks. For example, a function to read a file may start reading file and return the control to execution environment immidiately so that next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as parameter. So there is no blocking or wait for File I/O. This makes Node.js highly scalable, as it can process high number of request without waiting for any function to return result.
ViewHow to update a dependency using npm?
Answer:
Update package.json and change the version of the dependency which to be updated and run the following command.
C:\Nodejs_WorkSpace>npm updateView
How to uninstall a dependency using npm?
Answer:
Use following command to uninstall a module.
C:\Nodejs_WorkSpace>npm uninstall dependency-nameView
Name some of the attributes of package.json?
Answer:
Following are the attributes of Package.json
name - name of the package
version - version of the package
description - description of the package
homepage - homepage of the package
author - author of the package
contributors - name of the contributors to the package
dependencies - list of dependencies. npm automatically installs all the dependencies mentioned here in the node_module folder of the package.
repository - repository type and url of the package
main - entry point of the package
keywords - keywords
What is Package.json?
Answer:
Package.json is present in the root directory of any Node application/module and is used to define the properties of a package.
ViewHow to check the already installed dependencies which are globally installed using npm?
Answer:
Use the following command:
C:\Nodejs_WorkSpace>npm ls -gView
What is local installation of dependencies?
Answer:
By default, npm installs any dependency in the local mode. Here local mode refers to the package installation in node_modules directory lying in the folder where Node application is present. Locally deployed packages are accessible via require(). To install a Node project locally following is the syntax.
C:\Nodejs_WorkSpace>npm install expressView
What is global installation of dependencies?
Answer:
Globally installed packages/dependencies are stored in /npm directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but can not be imported using require() in Node application directly. To install a Node project globally use -g flag.
C:\Nodejs_WorkSpace>npm install express -gView
What is NPM?
Answer:
NPM stands for Node Package Manager. npm provides following two main functionalities:
Online repositories for node.js packages/modules which are searchable on search.nodejs.org
Command line utility to install packages, do version management and dependency management of Node.js packages.
© 2017 QuizBucket.org