Node Js Interview Questions

栏目: IT技术 · 发布时间: 4年前

内容简介:Here we listed down the most asked interview questions on Node js so that you don’t have to go anywhere. This is a one-stop destination for all your queries. We provide you the top 25 interview questions on Node js so you can ace your interview. Let’s just

Here we listed down the most asked interview questions on Node js so that you don’t have to go anywhere. This is a one-stop destination for all your queries. We provide you the top 25 interview questions on Node js so you can ace your interview. Let’s just look at the questions below.

1. What is Node js?

The first and most asked question is what is Node js? . Node js is an open-source server environment that uses javascript to make web software that is computationally simple but is easily accessed. It works really fast and can run on different platforms like Windows, Linux, Mac OsX, etc

2. What are some key benefits to Nodejs?

There are numerous benefits of Node js which are explained as follows.

  1. It is fast because it is built on Google chrome’s V8 JavaScript engine which makes it really fast.
  2. Node js has no buffering and no blocking while working. It output the data in chunks.
  3. It is Asynchronous meaning Nodejs never stops for an API to return the data. It is ready to take the next request.

3. Is Node js single-threaded? If yes, then why?

Well yes and actually no. NodeJS is single-threaded since no two functions can be run at the same time. Although, a running program called a process can have multiple threads. NodeJS runs only one program at a time to implement its asynchronous nature of program execution hence a single-threaded server environment but can a program can use multiple threads internally to yield optimal performance hence a multi-threaded server environment.

4. What type of applications you can build using Node js?

  • Streaming apps
  • Chat applications
  • Internet of things
  • Microservices
  • Collaboration tools
  • You just name it and we can build it using Node.js

5. How the content of a file is read by Node js?

The NodeJS’s fs (file-system) module provides an API to interact with the system files. The files can be read with multiple methods available to us. In the example below, we will be using the readfile method of fs module to read the contents of a file.

var fs = require(‘fs’);
 
fs.readFile(‘DATA’, ‘utf8’, function(err, contents) {
 
    console.log(contents);
 
});
 
console.log(‘after calling readFile’);
 
if you want to know in synchronous manner then have a look in this sample
 
var fs = require(‘fs’);
 
var contents = fs.readFileSync(‘DATA’, ‘utf8’);
 
console.log(contents);
 
 

6. Discuss the streams in Nodejs? And what are the different types of streams?

Streams are something that allows the reading and writing of data from source to destination in a continuous process.

Streams are of 4 types

·         <Readable> that promotes reading operation

·         <Writable> that promotes writing operation

·         <Duplex> that promotes above both

·         < Transform> is a kind of duplex stream which does the computation based on available input.

7. What is closure?

A closure is a function that is sustained in another scope that has access to all other variables in the outer scope.

8. Does Zlib use in Nodejs? If yes, then why?

Yes, Zlib used in Nodejs and Zlib was written by Jean-loup Gailly and Mark Adler . It is a cross-platform data compression library. You need to install a node- Zlib package in order to use Zlib in Nodejs. A sample is given below which shows the code to use Zlib. 

var Buffer = require(‘buffer’).Buffer;
 
var zlib = require(‘zlib’);
 
var input = new Buffer(‘lorem ipsum dolor sit amet’);
 
var compressed = zlib.deflate(input);
 
var output = zlib.inflate(compressed);

9. Discuss the globals in Node.js?

Globals basically comprise three words which are Global, Process and Buffer. Let’s discuss it one by one.

Global –  As the name is suggesting Global is something which has many things under its umbrella. So it’s a namespace object and act as an umbrella for all other objects < global>

Process – It is a specified type of Global and can convert Asynchronous function into an Async callback. It can be linked from anywhere in the code and it basically gives back the information about the application.

Buffer – Buffer is something that is known as a class in Nodejs to tackle the binary data.

10. Differentiate between Nodejs and Ajax?

Ajax is used on a specific section of a page’s content and update that specific portion rather than updating the full part of the content.

Nodejs, on the other hand, used for developing client-server applications. Both of the above serve different purposes and these are the upgraded implementation of JavaScript.

11. What is Modulus in Node Js?

Modules are a reusable block of code whose existence doesn’t impact alternative code in any means. it’s not supported by Javascript. Modules come in ES6. Modules are necessary for Maintainability, Reusability, and Namespacing of Code.

12. What do you mean by an event loop and how does it work?

An event loop tackles all the callbacks in any application. It is the vital component of Nodejs and the main reason behind the non- blocking I/O. Since Node.js is associate event-driven language, you’ll be able to simply attach an observer to an occurrence so once the event happens the callback is executed by the precise observer.

13. What is callback hell?

Callback Hell is additionally referred to as the Pyramid of Doom. it’s a pattern caused by intensively nested callbacks that square measure unreadable and unwieldy. It usually contains multiple nested request functions that successively build the code exhausting to browse and correct. it’s caused by improper implementation of the asynchronous logic.

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”);
 
        }
 
      });
 
    });
 
  });
 
});

14. What are the types of API functions in Nodejs?

There are mainly two types of API functions, one is blocking function and the other is non- blocking function .

Blocking function :  These functions implement synchronously and all other code is blocked from implementing until an I/O event that is being waited occurs.

For instance

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

Non-blocking Functions : These functions implement ASynchronoulsy and in this multiple, I/O calls can be executed without being waited for.

For instance

const fs = require(‘fs’);
 
fs.readFile(‘/file.md’, (err, data) => {
 
  if (err) throw err;
 
  console.log(data);


以上所述就是小编给大家介绍的《Node Js Interview Questions》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Operations

Web Operations

John Allspaw、Jesse Robbins / O'Reilly Media / 2010-6-28 / USD 39.99

A web application involves many specialists, but it takes people in web ops to ensure that everything works together throughout an application's lifetime. It's the expertise you need when your start-u......一起来看看 《Web Operations》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具