What are the benefits of using Node.js?
Following are main benefits of using Node.js:-
1-Aynchronous and Event Driven:-All APIs of Node.js library are aynchronous that is non-blocking. It essentially means a Node.js based server never waits for a API to return data. Server moves to next API after calling it and a notification mechanism of Events of Node.js helps server to get response from the previous API call.
3-Single Threaded but highly Scalable :- Node.js uses a single threaded model with event looping. Event mechanism helps server to respond in a non-bloking ways and makes server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and same program can services much larger number of requests than traditional server like Apache HTTP Server.
4-No Buffering :- Node.js applications never buffer any data. These applications simply output the data in chunks.
Is it free to use Node.js?
Yes! Node.js is released under the MIT license and is free to use.
Is Node a single threaded application?
Yes! Node uses a single threaded model with event looping.
What is REPL in context of Node?
REPL stands for Read Eval Print Loop and it represents a computer environment like a window console or unix/linux shell where a command is entered and system responds with an output. Node.js or Node comes bundled with a REPL environment. It performs the following desired tasks.
2-Eval - Takes and evaluates the data structure
3-Print - Prints the result
4-Loop - Loops the above command until user press ctrl-c twice.
What is the difference of using var and not using var in REPL while dealing with variables?
Use variables to store values and print later. if var keyword is not used then value is stored in the variable and printed. Wheras if var keyword is used then value is stored but not printed. You can use both variables later.
package.json is present in the root directory of any Node application/module and is used to define the properties of a package.
Why is Node.js Single-threaded?
Node.js is single-threaded for async processing. By doing async processing on a single-thread under typical web loads, more performance and scalability can be achieved as opposed to the typical thread-based implementation.
Explain callback in Node.js?
A callback function is called at the completion of a given task. This allows other code to be run in the meantime and prevents any blocking. Being an asynchronous platform, Node.js heavily relies on callback. All APIs of Node are written to support callbacks.
What is the difference between Node.js and Ajax?
Ajax is primarily designed for dynamically updating a particular section of a page’s content, without having to update the entire page.
Node.js is used for developing client-server applications.
Explain chaining in Node.js?
Chaining is a mechanism whereby the output of one stream is connected to another stream creating a chain of multiple stream operations.
What are “streams” in Node.js? Explain the different types of streams present in Node.js?
Streams are objects that allow reading of data from the source and writing of data to the destination as a continuous process.
There are four types of streams:
1.to facilitate the reading operation.
2.to facilitate the writing operation.
3.to facilitate both read and write operations.
4.is a form of Duplex stream that performs computations based on the available input.
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.
How Node prevents blocking code?
By providing callback function. Callback function gets called whenever corresponding event triggered.
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.
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.
Which module is used for file based operations?
fs module is used for file based operations.
var fs = require("fs")
Which module is used for web based operations?
http module is used for web based operations.
var http = require("http")
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.
Streams are objects that let you read data from a source or write data to a destination in continous fashion.
What is the purpose of __dirname variable?
The __dirname represents the name of the directory that the currently executing script resides in.