Node Flashcards
What is Node.js? Where can you use it?
“Node.js is a run-time JavaScript environment built on top of Chrome’s V8 engine. It uses an event-driven, non-blocking I/O model. It is lightweight and so efficient. Node.js has a package ecosystem called npm.
Node.js can be used to build different types of applications such as web application, real-time chat application, REST API server etc. However, it is mainly used to build network programs like web servers, similar to PHP, Java, or ASP.NET. Node.js was developed by Ryan Dahl in 2009.
“
Why use Node.js?
Node is fast, efficient and scalable, event driving, no i/o blocking, asynchronous, you can use the same language on the front and back end.
“Node.js makes building scalable network programs easy. Some of its advantages include:
It is generally fast
It almost never blocks
It offers a unified programming language and data type
Everything is asynchronous
It yields great concurrency
“
What are the features of Node.js?
“It is asynchronous and event driven
Super fast- being built on google chrome’s v8 engine
Single threaded but highly scalable
“
Node.js is a single-threaded but highly scalable system that utilizes JavaScript as its scripting language. It uses asynchronous, event-driven I/O instead of separate processes or threads. It is able to achieve high output via single-threaded event loop and non-blocking I/O.
How do you update NPM to a new version in Node.js?
“You use the following commands to update NPM to a new version:
$ sudo npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
npm@2.7.1 /usr/lib/node_modules/npm”
Why is Node.js Single-threaded?
Node.js is single-threaded for asynchronous 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 loads.
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 callback hell in Node.js?
“Callback hell is the result of heavily nested callbacks that make the code not only unreadable but also difficult to maintain. For example:
query(""SELECT clientId FROM clients WHERE clientName='picanteverde';"", function(id){ query(""SELECT * FROM transactions WHERE clientId="" + id, function(transactions){ transactions.each(function(transac){ query(""UPDATE transactions SET value = "" + (transac.value*0.1) + "" WHERE id="" + transac.id, function(error){ if(!error){ console.log(""success!!""); }else{ console.log(""error""); } }); }); }); });"
How do you prevent/fix callback hell?
“The three ways to prevent/fix callback hell are:
Handle every single error
Keep your code shallow
Modularize – split the callbacks into smaller, independent functions that can be called with some parameters then joining them to achieve desired results.
The first level of improving the code above might be:
var logError = function(error){ if(!error){ console.log(""success!!""); }else{ console.log(""error""); } }, updateTransaction = function(t){ query(""UPDATE transactions SET value = "" + (t.value*0.1) + "" WHERE id="" + t.id, logError); }, handleTransactions = function(transactions){ transactions.each(updateTransaction); }, handleClient = function(id){ query(""SELECT * FROM transactions WHERE clientId="" + id, handleTransactions); };
query(““SELECT clientId FROM clients WHERE clientName=’picanteverde’;”“,handleClient);
You can also use Promises, Generators and Async functions to fix callback hell.”
Explain the role of REPL in Node.js.
“As the name suggests, REPL (Read Eval print Loop) performs the tasks of – Read, Evaluate, Print and Loop. The REPL in Node.js is used to execute ad-hoc Javascript statements. The REPL shell allows entry to javascript directly into a shell prompt and evaluates the results. For the purpose of testing, debugging, or experimenting, REPL is very critical.
“
Name the types of API functions in Node.js.
“There are two types of functions in Node.js.:
Blocking functions - In a blocking operation, all other code is blocked from executing until an I/O event that is being waited on occurs. Blocking functions execute synchronously
For example:
const fs = require(‘fs’);
const data = fs.readFileSync(‘/file.md’); // blocks here until file is read
console.log(data);
// moreWork(); will run after console.log
The second line of code blocks the execution of additional JavaScript until the entire file is read. moreWork () will only be called after Console.log
Non-blocking functions - In a non-blocking operation, multiple I/O calls can be performed without the execution of the program being halted. Non-blocking functions execute asynchronously.
For example:
const fs = require('fs'); fs.readFile('/file.md', (err, data) => { if (err) throw err; console.log(data); }); // moreWork(); will run before console.log
Since fs.readFile () is non-blocking, moreWork () does not have to wait for the file read to complete before being called. This allows for higher throughput. “
What are the functionalities of NPM in Node.js?
“NPM (Node package Manager) provides two functionalities:
Online repository for Node.js packages
Command line utility for installing packages, version management and dependency management of Node.js packages”
What is the difference between Node.js and Ajax?
“Node.js and Ajax (Asynchronous JavaScript and XML) are the advanced implementation of JavaScript. They all serve completely different purposes.
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.”
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.
to facilitate the reading operation
to facilitate the writing operation
to facilitate both read and write operations
is a form of Duplex stream that performs computations based on the available input “
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 Globals in Node.js?
“Three keywords in Node.js constitute as Globals. These are:
Global – it represents the Global namespace object and acts as a container for all other objects. Process – It is one of the global objects but can turn a synchronous function into an async callback. It can be accessed from anywhere in the code and it primarily gives back information about the application or the environment. Buffer – it is a class in Node.js to handle binary data. "