Node Js Interview Questions
-
1 What is Node.js?
Node.js has become one of the most widely used server-side technologies today. Node.js came into existence in 2009 by Ryan Dahi when he decided to build a technology that will run on your machine as a standalone application. Now javascript is able to do much more than just making the web pages interactive.
Node.js is built on Google Chrome's Javascript V8 Engine. Which makes it much faster than any other technology. It also provides a rich library of a variety of javascript modules which make the development of web applications using Node.js much easier.
-
2 What are the features of Node.js?
There are a number of features that argue in the favor of Node.js. please find the explanation below,
-
Speed - As it is built on Google Chrome's V8 Engine, which makes Node.js very fast in code execution.
-
Asynchronous and Event-Driven - All the APIs created with Node.js are asynchronous or non-blocking. It generally means that it does not wait for the Api to return the response it certainly moves to the second API after calling the first API. It just keeps moving to the next API after calling it one by one and it uses a notification mechanism of the event to get the response data from the previously called the API.
-
Single-Threaded - Node.js follows single-threaded model along with the event looping mechanism.
-
Highly Scalable - Node.js has Event Mechanism which helps the server to respond asynchronously and this makes it highly scalable against all the other traditional servers which uses limited thread to handle the request.
-
Buffering - When it comes to data buffering, Node.js got immunity for this. It means that the application simply outputs the data in chunks without buffering any data.
-
Open Source - As Node.js is an open-source technology, there are a number of amazing models that are introduced by Node.js community which makes a Node.js Application much better.
-
-
3 Which are the technologies used to write Node.js?
Node.js is a programming platform written in Javascript, C and C++. It uses Google Chrome's V8 Engine to convert Javascript code to C++;
-
4 What is Google Chrome's V8 Engine?
V8 is a Javascript engine written in C++ and developed by Google. It is an open-source javascript engine used in Google Chrome and Node.js. V8 engine was developed to enhance the performance of javascript execution in the browser. Instead of using the interpreter V8 Engine translates Javascript code to machine code in order to enhance the overall performance. It uses JIT (Just-In-Time) compiler to compile the Javascript code as a machine code.
-
5 How to check the installed version of Node.js?
You can use node -v command to check the exact installed version of Node.js.
-
6 Explain modules in Node.js?
Modules in Node.js are blocks of reusable code or functionality organized in single or multiple javascript files. Its existence can not affect other modules or globe scope as each module has its own context and can be placed in a separate folder or file. Modules are introduced in ECMAScript6.
There are three types of modules available in Node.js,
-
Core Modules
-
Local Modules
-
Third party Modules
-
-
7 Explain the functionality of require() in Node.js?
In Node.js, require() provides the functionality to include external modules in the easiest way. It's built-in functionality of the CommonJs module system which is used in Node.js. It basically reads the included javascript file, executes it and returns the export object.
require('path');
-
8 Explain the functionality of module.exports in Node.js?
Module.exports is a special type of object included by default in every js file of Node.js application. Methods and properties defined in any module can not be directly accessed. In order to make these module members accessible, we need to export those methods and properties using module.exports object.
Please find the example below,
// example.js
var example=function(){
console.log("hello World");
}
module.exports=example;
// app.js
var example=require('./greet.js');
example();
-
9 Explain events in Node.js?
While working on a computer system, every action performed by a user is an event. Like when a file is opened or a connection is made.
As per the official documentation of Node.js, it has an event-driven architecture to perform asynchronous tasks. Node.js has an 'events' module that provides the functionality to emit named events that causes the calling of corresponding callbacks or methods. These callbacks wait for a specific event to occur and the time when this event triggers, all the callbacks functions subscribe to this event gets fired one by one in an orderly manner.
There are two types of events are available in Node.js,
-
System Events
-
Custom Events or User Defined Events
-
-
10 How to create a simple server in Node.js?
Please find below the code to create a simple server which will listen to port 4200,
var http =require('http');
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello Worldn');
}).listen(4200,'127.0.0.1');
-
11 What is ExpressJS?
ExpressJS is basically a web application framework developed for Node.js. It offers a variety of important features that makes application development much easier as compared to Node.js only. We can build Single-page, Multi-page and Hybrid web applications using ExpressJS.
Advantage of using ExpressJS:
-
Makes the application development much easier as compared to application development with Node.js alone.
-
Configuration and customization is easy with ExpressJS.
-
It offers a variety of middleware modules which will be helpful in handling request and response.
-
Easy connectivity with most of the popular databases including MongoDB, Mysql, Redis etc.
-
It provided the functionality to create REST API Server.
-
It offers you the functionality to create an error-handling middleware.
-
-
12 How to install ExpressJS?
You can install ExpressJS using NPM. The command given below will install the ExpressJS globally in your system so that every node application will be able to use it.
npm install -g express
If you want to install the ExpressJS locally then below command will be helpful,
C:MyNodeJSApplication> npm install express --save
In the above command --save will update the package.json file with the ExpressJS dependency.
-
13 Which are the arguments available in ExpressJS Router Handler?
The arguments available in ExpressJS router handler are given below,
Req: Request Object
Res: Response Object
Next (Optional) : This last argument is used to pass the control to another subsequent route handler. It is very useful, when there is a chain of handlers.
-
14 How to manage configuration properties in ExpressJS?
There are two ways to handle the configuration properties in ExpressJS.
With Require:
-Create config.json file inside config folder to the root of you project
-Add all your configuration properties in config.json file
-Now, use require to get the config.json file
With process.ENV:
-Create a .env file inside the root of your project folder
-Add all the configuration properties in .env file
-Now, you can use any of the property in your server.js file
-
15 What is the key difference between fs.createReadStream and fs.readFile in Node.JS?
fs.createReadStream:
1. fs.CreateReadStream reads the complete file in chunks of sizes that user-specified.
2. The client receives data faster since it is sent in chunks to the client while reading the file.
3. In case there are too many requests at the same time, fs,createReadStream is very handy. It pipes all the content directly to the HTTP response object.
fs.readFile:
1. Before presenting the file to the user, it reads the entire file into its memory.
2. As fs.readFile reads the entire file in memory before sending it to the client. This can be negligible in case of small files but i will make a huge time difference when the file is big and the disk is slow.
3. It will not scale, in case there are too many requests at the same time because it will try to load all the files in the memory at once.
-
16 How to enable CORS in Node.js?
Cross-Origin Resource Sharing (CORS) is a mechanism that allows HTTP headers to tell a browser to allow a web application running at one domain to get access to required resources from another domain of a different server.
Sample code to enable Cross Origin Resource Sharing (CORS),
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
-
17 How does Node.js read a file?
Node.js uses fs (File System) class to read a file. First, we need to load the fs class using require function and then we will be able to use all the functionality of fs class. Node.js can read a file in both ways i.e. Synchronous (Blocking) or Asynchronous (Non-Blocking). Please find below the sample code for both the methods,
Reading a file asynchronously (non-blocking):
var fs = require('fs');
fs.readFile('DATA', 'utf8', function(err, contents) {
console.log(contents);
});
console.log('after calling readFile');
Reading a file synchronously (blocking):
var fs = require('fs');
var contents = fs.readFileSync('DATA', 'utf8');
console.log(contents);
-
18 Explain Streams in Node.js?
Stream is a special type of object in Node.js, which provides you the ability to read data from a source or writing data to a destination in a continuous manner.
There are 4 types of Streams available in Node.js
Readable − Used for read operation.
Writable − Used for writing operations.
Duplex − Used for both read and write operation.
Transform − Kind of duplex stream where the output is computed based on input
Each Stream throws several events at different instances of time as it is an instance of EvenEmitter.
Some of the common events are given below. these events are triggered when
data − When there is data available to read.
end − When there is no more data to read.
error − When there is any error receiving or writing data.
finish − When all the data has been flushed to the underlying system.
-
19 Explain the revealing module pattern?
The revealing module pattern is the most perfect and clean way to structure your code and keep it protected. In this module pattern, everything stays hidden and private.
Please find below the example,
var example = "Hi i am a node beginner.";
function exam(){
console.log(example);
}
module.exports = {
greet:greet;
}
## Usage
var greet = require('./example').exam;
exam();
//Output: Hi I am a node beginner.
-
20 How to decide when to use Node.js for your application?
Node.js is ideal to use for developing event-based real-time applications or streaming applications that require minimum CPU requirements. If you have a high-performance server that is able to handle thousands of requests simultaneously then Node.js is the best option for you.
Chat Application
Game Servers
Collaborative Environment: Node.js is an ideal environment, where multiple users work together and upload their documents, modify them etc. Node.js will be able to handle all these requests.
Advertisement Servers: Node.js is ideal for those servers, handling thousands of requests to download the advertisement from a central host.
Streaming Servers: Node.js is best in the case of multimedia streaming servers where the user's send the request to the server to download media contents.
The node.js interview questions let you know from the basics to advanced level, which helpful to get stronger in the programming platform. The main aim of offering interview questions and answers specifically targeted the fresher to enter into the top companies. Now, those who utilize the interview questions surely realize the importance and benefits. Whatever, you have already little bit experienced try out web developer interview questions and answers for freshers give the satisfaction. You can learn what the feasible questions are asked in the interviews before you attend any of the interviews. We guide you to move into the right way and make future day’s unlimited pleasure after you placed in the best job. The node.js interview questions and answers are available now and waiting for you to make use of it. So, why you are waiting for spend your valuable time with us and make the dream true. Already, many candidates who are new in the programming stay connected with us and make learning easier. We give the best solutions for all queries related to the job and programming at anytime and from anywhere.