Nodejs Interview Questions with Answers

Interactive Online Courses: Elevate Skills & Succeed Enroll Now!

In this part of interview questions on node js, you will have advanced questions with answers for different node js topics. This includes some tough theoretical questions and coding problems that are asked during interview. Let’s start!!!

Nodejs interview questions with Answers

1) What is a queue microtask?
Ans Microtask is a small function. When the javascript stack is empty and the callback function completes then this function executes.

The queueMicrotask() method is used to execute such functions after the callback function completes successfully.

2) How to create a server in nodejs which will return DataFlair.
Ans. Below is the code for the same:

let http = require('http');
http.createServer(function (request, response) {
    response.write("Welcome to DataFlair");
    response.end();
}).listen(5000);
console.log('Server is running on port 5000');

3) How to create a web client so that it can send requests to the server?
Ans. Below is the code:

let http = require('http');
let options = {
    host: 'localhost',
    port: '5000',
    path: '/'
};
const callback = function (response) {
    let content = '';
    response.on('data', function (data) {
        content += data;
    });
    response.on('end', function () {
        console.log(content);
    });
}
let req = http.request(options, callback);
req.end();

 

4) Explain the os methods that you know?
Ans.

  • os.tmpdir():it will return the operating system’s default directory for temp files.
  • os.hostname(): It will return the hostname of the operating system.
  • os.type(): It will return the operating system name.
  • os.platform(): It will return the operating system platform.
  • os.release(): It will return the operating system release.
  • os.uptime(): It will return the system uptime in seconds.

5) How does node js handle blocking i/o operations?
Ans. Nodejs has an event loop that can handle the I/O operations in an asynchronous fashion without blocking the main function. For example, if some network call occurs it will be scheduled in the event loop instead of the main thread. And if there are multiple such I/O calls each one will be queued . Thus I/O operations are handled in a non blocking way.

6) Explain middleware.
Ans. Middleware are those parts of code that executes between the request and logic. It is used to enable rate limit, routing, authentication etc. There is third-party middleware also such as body-parser or we can also write in our own.

7) What is EventEmitter in node js?
Ans. EventEmitter is a Node.js class that contains the objects that are capable of emitting events. It can be done by attaching named events that are emitted by the object using an eventEmitter.on() function.

8) What is rest Architecture and restful web services?
Ans Rest Architecture is a web standards-based architecture which uses HTTP Protocol. Every component is a resource and these resources are accessed by an interface using HTTP methods.

A web service is used for exchanging data between applications. Web services which are based on REST architecture are called restful web services. It uses http methods to implement rest architecture.

9) How can you delete or remove in a rest api?
Ans. Below is the code:

let express = require('express');
let app = express();
let fs = require("fs");
app.get('/employee', function (req, res) {
    fs.readFile(__dirname + "/" + "employee.json", 'utf8', function (err, data) {
        console.log(data);
        res.end(data);
    });
}
let employee= {
    "employee3": {
        "name": "raj",
        "id": 3
    }
}
app.post('/addemployee', function (req, res) {
    fs.readFile(__dirname + "/" + "employee.json", 'utf8', function (err, data) {
        data = JSON.parse(data);
        data["employee3"] = user["employee3"];
        res.end(JSON.stringify(data));
    });
})
const id = 2;
 
app.delete('/deleteemployee', function (req, res) {
    fs.readFile(__dirname + "/" + "employee.json", 'utf8', function (err, data) {
        data = JSON.parse(data);
        delete data["employee" + 2];
        res.end(JSON.stringify(data));
    });
})
 
app.listen(5000, function () {
    console.log("server running on port 5000")
})

10) What do you understand by components of a nodejs application?
Ans

  • Importing modules: To include anything in nodejs we use the require() function.
  • Creating server: This will listen to the client requests. Mostly http methods to create the server.
  • Request and response: The server will listen to the http requests made by the client and in turn will send a response for that request.

11) Why are there advantages of using mysql with nodejs?
Ans. Because:

  • It is a free database.
  • It supports most of the operating system.
  • This can be used with almost all the languages.
  • Fast even when it has large data.

12) Write a code to connect to a mysql and create a database.
Ans Below is the code:

let mysql = require('mysql');
let con = mysql.createConnection({
    host: "localhost",
    user: "root",
});
con.connect(function (err) {
    if (err) throw err;
    con.query("CREATE DATABASE employee", function (err, result) {
        if (err) throw err;
        console.log("Database created successfully");
    });
});

13) How can you limit user to get upto a certain amount of result in sql?
Ans. Below is the code:

let mysql = require('mysql');
let con = mysql.createConnection({
    host: "localhost",
    user: "root",
    database: "employee"
});
 
con.connect(function (err) {
    if (err) throw err;
    var sql = "SELECT * FROM employee LIMIT 1";
    con.query(sql, function (err, result) {
        if (err) throw err;
        console.log(result);
    });
});

14) How can you connect to mongodb and create a database?
Ans. below is the code:

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/employee";
MongoClient.connect(url, function (err, db) {
    if (err) throw err;
    console.log("Database created!");
    db.close();
});

15) Write a code for creating collections in mongodb.
Ans. Below is the code:

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
MongoClient.connect(url, function (err, db) {
    if (err) throw err;
    var dbo = db.db("employee");
    dbo.createCollection("department", function (err, res) {
        if (err) throw err;
        console.log("Collection created!");
        db.close();
    });
});

Summary:

This article covered some of the advanced node js questions with answers.This was all about node js subjective questions for interview. Make sure to revise the questions and other important topics.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

Your email address will not be published. Required fields are marked *