
Node.js is becoming very popular technology for developing web applications these days. Some of the main reasons of popularity of Node.js are as follows:
In Event Driven Programming (EDP), the flow of a software program is based on the events, mostly user actions, like- Mouse clicks, key presses, sensor outputs.
EDP is mainly used in Graphic User Interface (GUI) projects.
In EDP, there is a main loop that keeps running and listening to events. As and when an event is detected, a callback function is triggered. This callback function is related to the event.
Some of the main advantages of using Node.js are as follows:
Node.js can be used for building a wide-variety of applications. Some of the main types of applications that can be easily built with Node.js are as follows:
Node.js is based on single threaded architecture due to the reason that it runs on Google’s V8 engine which is a single threaded architecture.
Over the time, single threaded architecture of Node.js provides much better performance than one thread per request architecture.
Node.js mainly provides following two types of APIs:
Yes, it is possible to run Node.js on Windows environment. We have to download windows .msi file to run Node.js applications on Windows.
Node.js is based on Event Driven programming model. At the heart of Node.js is an Event Loop. This Event Loop listens to events. On each event a call back function is called. This call back function is asynchronous in nature.
When we start Node.js, it internally starts the Event Loop.
Although Node.js is getting popular for certain applications, it has some disadvantages as well. Some of these are as follows:
In Node.js we can use asynchronous calls to handle blocking I/O operations. In an asynchronous main thread of our program is not waiting for an I/O operation to finish.
Let say we want to get some information from Database, we just fire and event. Our main program keeps executing the steps after that event. In the meantime when we receive output from database read, it emits another event. This event leads to further processing on the data retrieved from the database.
Where as our main program keep on executing all the time.
Asynchronous and non-blocking look very similar in nature but there is a subtle difference here.
Asynchronous means that our API will return immediately after calling it. In the background it will start a process to fulfill the request. Once that process is complete, our work is done. But our main thread does not wait for that process to complete.
Non-blocking means if our API cannot complete the work, it returns an error immediately. Based on the response, success of error, we can decide if we want to make another call to same API or continue with the next operation in our main flow. We can create a wait mechanism in a non-blocking operation.
Both Node.js and Ajax use Javascript in the backend implementation. Due to this, they appear similar in nature. But there are fundamental differences between these two.
Ajax is simply Asynchronous Javascript and XML. It is a client-side technology. It is mainly used for asynchronously handling the page refresh requests. Sites like Facebook, Gmail etc. use Ajax to implement dynamically self-loading pages. Ajax is executed in client browser.
Node.js is a server-side technology implemented in Javascript. It is used for developing enterprise software. Node.js is not executed in client browser. It executes in server.
Both Node.js and AngularJS use Javascript in the implementation. Due to this, they appear similar in nature. But there are fundamental differences between these two.
AngularJS is a web framework that is implemented in Javascript. It is used to build front-end web applications with powerful back-end support. In AngularJS we use tags to create single page applications. It is based on Model View Controller (MVC) design pattern.
Node.js is a platform to create server-side software applications. It is based on Google’s V8 Javascript engine. We can use it to create a highly scalable system with large data usage.
We can use require() call to import external libraries in Node.js.
Node.js has a built-in caching mechanism to load modules. If we call require() multiple times to load same module, the module will be cached the first time it is loaded. This provides significant performance improvement.
REPL stands for Read Eval Print Loop in Node.js. We have to use require(‘repl’) to load this module.
There is a repl.REPLServer class in repl module. This server accepts input from user. This input is evaluated by REPLServer and an appropriate action is taken on that.
In general it supports many functions that are popular in Unix Command Line Interface. Like- Automatic completion of input, editing in-line, multi-line input, Control C, Control D etc.
Some of the popular REPL commands in Node.js are as follows:
NPM or npm is Node Package Manager in Node.js. There are two main uses of NPM:
In a Node.js application we do multiple I/O operations in parallel. Since I/O operations do not share data between threads, we can execute these in parallel. Once I/O is complete, it emits event that can be handled by the Event Loop. Due to handling of multiple I/O in parallel Node.js application can be scaled easily.
Scalability in Node.js is more applicable to web-services where we have a large amount of I/O operations.
One limitation of Node.js scalability is that we cannot use it for an application with parallel processes that share data.
In Node.js module.exports is same as exports. It is an object created by Module system.
We have to do module.exports immediately. It cannot be done in asynchronous mode in a callback.
In a require() call Node.js returns module.exports object.
We can use Tracing to see the events generated by V8 engine, Node core and userspace code.
We can enable Tracing in a Node application by passing the --trace-events-enabled flag when starting a Node.js application.
Once this flag is enabled, we can see the log files with the Trace events in a tab of Chrome browser.
The latest implementation of Node.js includes an out-of-process debugging utility. We can access it via a TCP-based protocol and a built-in debugging client.
We have to start Node.js with the debug argument followed by the path of the script to start debugging.
We can put debugger keyword in a statement to enable a breakpoint. On running the debugger, there will be breakpoint at that location.
We can also watch expressions by using watch().
There is a child_process module in Node.js that can be used to spawn child processes.
We can use commands like spawn(), exec(), fork() etc to create new Child Processes.
These child processes can be synchronous as well as asynchronous.
For shell script automation, synchronous child process can be used.
In asynchronous mode, we can specify a callback that will be called when Child process terminates.
We can use cluster module to take advantage of multi-core system in a Node.js application.
We can create child processes that share server ports by using Cluster module.
These processes communicate via IPC.
Cluster module can distribute the incoming connections in Round Robin approach or in Interested Worker approach. In Round Robin approach, master distributes connections to workers in a round-robin fachion.
In Interested Worker approach, master distributes connections to interested workers only. Then workers can directly accept the connections.
Based on our application needs we can kill worker threads on need basis, without affecting other workers.
A Closure is an inner function that can access variables of outer function.
We use Closures extensively in Node.js. These are the main building blocks of asynchronous and non-blocking architecture of Node.js.
The inner function has access to not only its own variables but also to the parameters of outer function.
One simple way to create a closure is to include a function inside another function in JavaScript.
E.g.
function printName (name) {
var address = “My name is ";
function addressMe () {
return address +” “ + name;
}
return addressMe ();
}
printName(“James”); // My name is James
We even use Closures in Jquery also.
A Buffer in Node.js is used to read and manipulate binary data streams. It can be used with TCP streams or in file system operations.
As per Node documentation, a Buffer is similar to an array of integers.
A Buffer has raw memory allocated outside the V8 engine heap.
When we create a Buffer, its size is specified and this size cannot be changed later.
Since Buffer class is global, any part of the program can access and use it.
Node.js provides utility method buffer.toJson() to convert a buffer data into JSON format. We can use it to convert Buffer contents to JSON format.
In Node.js __filename is the name of the current module. It is the absolute path of the current module file.
As per documentation, the __filename is not necessarily the same as the file name used in the command line for a main program in Node.js.
In general, __filename is local to a module.
Node.js provides a useful module timers (aka Timers) to schedule functions at a later point of time.
Timer functions are global. Therefore there is no need to call require(‘timers’).
These API are similar to the APIs in web browser for timers tasks.
Some of the important APIs in Timers module of Node.js are as follows:
EventEmitter is a class defined in events module in Node.js. This class is used for emitting the ‘newListener’ event when a new listener is added. On removing a listener ‘removeListener’ event is emitted.
EventEmitter also maintains the list of listeners that are added to it.
In Node.js, EventEmitter is an important class to interact with listeners in a program.
Node.js provides net.Socket class in net module that can be used for creating a new socket object.
Socket is an abstraction of TCP socket. In this implementation we can use it as a duplex stream.
A client can create net.Socket and use it to connect to a server. Node.js can also create net.Socket and pass it in a connection to client.
We have to set readable and writable flags to allow reads and writes on this socket.
Some of the important events of net.Socket in Node.js are as follows:
Yes, we can build a REST (Representational State Transfer) service in Node.js.
We can use ExpressJS framework for this purpose.
We can provide implementation for GET, POST and PUT requests to handle REST services on a resource.
A simple implementation with express is as follows:
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello Node!’);
});
app.listen(3000);
We use DNS module in Node.js to get the utilities for domain name resolution. There are two sets of functions in this module.
Yes, we can build a REST (Representational State Transfer) service in Node.js.
Some of the important command line options in Node.js are as follows:
Node.js is a server side Javascript based platform. In Node.js there is a V8 engine that is like a virtual machine. This engine executes Javascript code written in Node.js. It has a single event loop and non-blocking I/O. This helps in providing very high throughput.
Node.js heavily depends on Callback functions. But at times developers create programs that create heavily nested Callbacks. This leads to unreadable spaghetti code that is difficult to comprehend.
We can use divide and conquer approach to avoid Callback Hell. We have to create loosely coupled modules in our Node.js program that handle specialized functions.
With each module, we define independent functions in which we can pass parameters.
We can create a handler for uncaughtexception event in Node.js. This handler can handle the unhandled exceptions.
But this is method is not a professional method to handle unhandled exceptions. In general, we should modularize our program and create domains. Any unhandledexception coming from a domain has to be handled at that level. This helps in handling the exception before letting it reach at Process level.
A Callback function is an important part of Event driven programming in Node.js. We use Callabck functions to handle multiple requests made to the server. With a Callback function we can handle events in an Asynchronous way.
Let say we have a large list of numbers to sort. In Node.js we can pass this list to a function with the callback. The server can keep on handling the next request. Once the sort function finishes its work, it calls the callback function to inform server and emits an event. Based on this event further actions can be taken.
Some of the most popular modules of Node.js are as follows:
In readFile function Node will read the file completely and load it in the memory. Whereas in createReadStream function, the file is read as a stream.
By using createReadStream function we can read file from any random position that is passed as an argument. We can pass start and end parameters to createReadStream function.
We use QueryString module to handle and process URL query strings. This module provides utilities to parse URLs and format URL query strings.
Some of the useful methods are querystring.escape(), querystring.parse(), querystring.stringify(), querystring.unescape() etc.
We can use OS module utilities to get the amount of free memory on the server.
The function to use is os.freemem(). This function gives us the amount of free system memory in bytes as an integer.
There are Global objects in Node.js that are accessible to all the parts and modules of Node application. Some of the Global objects are as follows:
Some of the important security mechanisms available in Node.js are as follows:
Zlib is a module in Node.js that provides compression and decompression utilities based on Gzip and Deflate/Inflate. Since there is a large amount of I/O in a Node.js application, it makes sense to use compression and decompression to save bandwidth and computing time.
We can use string_decoder module APIs to decode buffer objects. This module provides utilities to decode a buffer in a way that preserves encoded multi-byte UTF-8 and UTF-16 characters.
In this way we can convert the Buffer contents into readable String.
We use Assert module to implement simple unit tests in a Node.js application. Assert module has functions like assert.deepequal() , assert.deepstrictequal() etc functions to write different unit testcases.
Some of the standard Javascript errors are as follows:
Node.js is one of the most popular Javascript engine in technology world. There is a growing demand for Software Engineer jobs in Node.js. Many fortune 500 organizations use Node.js. Big companies like Amazon, Netflix, Google etc use Node.js based UI. This course is designed to help you answer interview questions on Node.js.
Software Engineers with Node.js knowledge may get more salary than others with similar qualifications without Node.js knowledge.
In this course, you will learn how to handle interview questions on Node.js in Software Deployment and Development. We will explain you the important concepts of Node.js.
You will also learn the benefits and use cases of Node.js in this course.
What is the biggest benefit of this course to me?
Finally, the biggest benefit of this course is that you will be able to demand higher salary in your next job interview.
It is good to learn Node.js for theoretical benefits. But if you do not know how to handle interview questions on Node.js, you can not convert your Node.js knowledge into higher salary.
What are the topics covered in this course?
We cover a wide range of topics in this course. We have questions on Node.js best practices, internal details, Node.js tricky questions etc.
How will this course help me?
By attending this course, you do not have to spend time searching the Internet for Node.js interview questions. We have already compiled the list of the most popular and the latest Node.js Interview questions.
Are there answers in this course?
Yes, in this course each question is followed by an answer. So you can save time in interview preparation.
What is the best way of viewing this course?
You have to just watch the course from beginning to end. Once you go through all the videos, try to answer the questions in your own words. Also mark the questions that you could not answer by yourself. Then, in second pass go through only the difficult questions. After going through this course 2-3 times, you will be well prepared to face a technical interview in Node.js.
What is the level of questions in this course?
This course contains questions that are good for a Fresher to an Architect level. The difficulty level of question varies in the course from a Fresher to an Experienced professional.
What happens if Node.js technology changes in future?
From time to time, we keep adding more questions to this course. Our aim is to keep you always updated with the latest interview questions on Node.js.
What are the sample questions covered in this course?
Sample questions covered in this course are as follows: