Node Js

[vc_row full_width=”stretch_row” css=”.vc_custom_1559286923229{background-color: #f6f6f7 !important;}”][vc_column width=”1/2″][vc_tta_accordion color=”peacoc” active_section=”1″][vc_tta_section title=”What is Node.js? Where can you use it?” tab_id=”1559286383409-ab730398-6c03″][vc_column_text]Node.js is a server side scripting based on Google’s V8 JavaScript engine. It is used to build scalable programs especially web applications that are computationally simple but are frequently accessed.

You can use Node.js in developing I/O intensive web applications like video streaming sites. You can also use it for developing: Real-time web applications, Network applications, General-purpose applications and Distributed systems.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Why use Node.js?” tab_id=”1559286522681-3bf94e12-e7b7″][vc_column_text]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

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are the features of Node.js?” tab_id=”1561382593569-b1979b66-b066″][vc_column_text]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.[/vc_column_text][/vc_tta_section][vc_tta_section title=”How else can the JavaScript code below be written using Node.Js to produce the same output?” tab_id=”1561382595833-dd54d407-26c0″][vc_column_text]console.log(“first”);
setTimeout(function() {
console.log(“second”);
}, 0);
console.log(“third”);

Output:

first
third
second

In Node.js version 0.10 or higher, setImmediate(fn) will be used in place of setTimeout(fn,0) since it is faster. As such, the code can be written as follows:

console.log(“first”);
setImmediate(function(){
console.log(“second”);
});
console.log(“third”);[/vc_column_text][/vc_tta_section][vc_tta_section title=”How do you update NPM to a new version in Node.js?” tab_id=”1561382597303-5168678c-55b9″][vc_column_text]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[/vc_column_text][/vc_tta_section][vc_tta_section title=”Why is Node.js Single-threaded?” tab_id=”1561382598718-1fee5a6b-29dd”][vc_column_text]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.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Explain callback in Node.js.” tab_id=”1561382602352-48d936eb-64df”][vc_column_text]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.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is callback hell in Node.js?” tab_id=”1561382603416-a2e0c7df-e6f8″][vc_column_text]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”);[/vc_column_text][/vc_tta_section][vc_tta_section title=”How do you prevent/fix callback hell?” tab_id=”1561382604362-41fc1dd4-d143″][vc_column_text]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.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Explain the role of REPL in Node.js.” tab_id=”1561382605426-bedbe54f-bb01″][vc_column_text]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.[/vc_column_text][/vc_tta_section][/vc_tta_accordion][/vc_column][vc_column width=”1/2″][vc_tta_accordion color=”peacoc” active_section=”1″][vc_tta_section title=”What are the functionalities of NPM in Node.js?” tab_id=”1561382561432-7f73ef2a-cc67″][vc_column_text]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

[/vc_column_text][/vc_tta_section][vc_tta_section title=”Name the types of API functions in Node.js.” tab_id=”1561382561455-654071d3-eb53″][vc_column_text]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.[/vc_column_text][/vc_tta_section][vc_tta_section title=” Which is the first argument typically passed to a Node.js callback handler?” tab_id=”1561382611424-56181e07-6453″][vc_column_text]Typically, the first argument to any callback handler is an optional error object. The argument is null or undefined if there is no error.

Error handling by a typical callback handler could be as follows:

function callback(err, results) {
// usually we’ll check for the error before handling results
if(err) {
// handle error somehow and return
}
// no error, perform standard callback handling
}[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is the difference between Node.js and Ajax?” tab_id=”1561382613753-7c9c9136-4ca1″][vc_column_text]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.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Explain chaining in Node.js.” tab_id=”1561382614729-6b63842b-62b1″][vc_column_text]Chaining is a mechanism whereby the output of one stream is connected to another stream creating a chain of multiple stream operations.[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are “streams” in Node.js? Explain the different types of streams present in Node.js.” tab_id=”1561382615672-42dd66a8-6425″][vc_column_text]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.

  •  <Readable> to facilitate the reading operation
  • <Writable> to facilitate the writing operation
  • <Duplex> to facilitate both read and write operations
  • <Transform> is a form of Duplex stream that performs computations based on the available input

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are exit codes in Node.js? List some exit codes. ” tab_id=”1561382616984-e392adb7-34cd”][vc_column_text]Exit codes are specific codes that are used to end a “process” (a global object used to represent a node process).

Examples of exit codes include:

  • Unused
  • Uncaught Fatal Exception
  • Fatal Error
  • Non-function Internal Exception Handler
  • Internal Exception handler Run-Time Failure
  • Internal JavaScript Evaluation Failure

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What are Globals in Node.js?” tab_id=”1561382618152-4fb6fc3a-9883″][vc_column_text]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 <global> 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.

[/vc_column_text][/vc_tta_section][vc_tta_section title=”What is the difference between AngularJS and Node.js?” tab_id=”1561382619930-3767e1f0-f3d6″][vc_column_text]Angular.JS is a web application development framework while Node.js is a runtime system.[/vc_column_text][/vc_tta_section][vc_tta_section title=”Why is consistent style important and what tools can be used to assure it?” tab_id=”1561382620762-261798c4-b2da”][vc_column_text]Consistent style helps team members modify projects easily without having to get used to a new style every time. Tools that can help include Standard and ESLint.[/vc_column_text][/vc_tta_section][/vc_tta_accordion][/vc_column][/vc_row][vc_row full_width=”stretch_row” overlay_color=”rgba(12,12,12,0.58)” css=”.vc_custom_1528340415800{padding-bottom: 104px !important;background-image: url(https://wordpresslms.thimpress.com/wp-content/uploads/sites/4/2017/06/layer-532.jpg?id=231) !important;background-position: center !important;background-repeat: no-repeat !important;background-size: cover !important;}” el_class=”overflow-visible become-teacher”][vc_column width=”1/2″][vc_column_text el_class=”align-right”]

Please login to send your request!
[/vc_column_text][/vc_column][vc_column width=”1/2″ el_class=”talk”][vc_column_text el_class=”thim-content-talk”]I’m a Copywriter in a Digital Agency, I was searching for courses that’ll help me broaden my skill set. Before signing up for Rob’s.[/vc_column_text][/vc_column][/vc_row]

WhatsApp us