Node.js is a server-side technology that you have to be proficient in to become the ideal MEAN stack developer. It is one of the most rewarding web development tools to learn, not only because of the lucrative salary—averaging $100,000/ year—but also the fast and continued growth of job postings requiring knowledge of the technology.
If you have a MEAN stack developer interview lined up or are anticipating interviews soon, expect to face cutthroat competition. The 20 interview questions outlined below should help you prepare adequately to answer every question asked confidently.
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");
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
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.
A callback function is called after a given task. It 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.
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");
}
});
});
});
});
The three ways to prevent/fix callback hell are:
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.
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.
There are two types of functions in Node.js.:
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
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.
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
}
NPM (Node Package Manager) provides two functionalities:
Node.js and Ajax (Asynchronous JavaScript and XML) are the advanced implementations of JavaScript. They all serve entirely 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.
Chaining is a mechanism whereby the output of one stream is connected to another stream creating a chain of multiple stream operations.
Streams are objects that allow the reading of data from the source and writing of data to the destination as a continuous process.
There are four types of streams.
Since every question has more than one answer, feel free to personalize your answers as much as possible, especially when you have work experience to relate the answers to. In case you need a refresher in Node.js, or if you do not have any training in it, you should consider Simplilearn’s Node.js certification training. This course provides an in-depth knowledge of concepts such as shrink-wrap, asynchronous programming, Node Packet Manager (NPM), and more.
Name | Date | Place | |
---|---|---|---|
Node.js Training | 14 Dec -18 Jan 2020, Weekend batch | Your City | View Details |
Srihari Sasikumar is a Product Manager with over six years of experience in various industries including Information Technology, E-Commerce, and E-Learning. Srihari follows the key trends in Big Data, Data Science, Programming & AI very closely.