Ans.Node.js is an open-source, server-side JavaScript runtime environment built on the V8 JavaScript engine by Google. It allows executing JavaScript code outside of a browser, enabling server-side development.
2
How does Node.js handle asynchronous requests ? Ans.Node.js uses an event-driven, non-blocking I/O model. It employs the event loop, which allows it to efficiently handle concurrent requests without blocking the execution of other operations.
Ans.NPM (Node Package Manager) is a package manager for Node.js. It comes bundled with Node.js installation and is used to install, manage, and share reusable JavaScript code packages/modules.
4
How do you import modules in Node.js?Ans.In Node.js, you can use the require() function to import modules. For example, const fs = require('fs'); imports the 'fs' module for file system operations.
5
How can you handle errors in Node.js?Ans.In Node.js, error handling can be done using try-catch blocks or by using error-first callbacks. Additionally, you can use promise rejections or async/await with try-catch for handling asynchronous errors.
6
What are streams in Node.js?Ans.Streams are objects used for handling continuous data flows in Node.js. They allow data to be read or written in chunks, which enhances performance and memory efficiency for handling large data sets.
7
Explain the difference between process.nextTick() and setImmediate().Ans.process.nextTick() and setImmediate() are both used to schedule asynchronous code execution in Node.js. However, process.nextTick() executes before the I/O event loop, whereas setImmediate() executes after the I/O event loop.
8
What is middleware in Express.js?Ans.Middleware in Express.js is a function that has access to the request and response objects in the application's request-response cycle. It can modify the request/response objects, invoke the next middleware, or end the request-response cycle.
9
How can you handle form data in Express.js?Ans.
Express.js provides the body-parser middleware to handle form data in the request object. You can use it as follows:const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
10
What is the purpose of the package.json file?Ans.The package.json file is a manifest file used in Node.js projects to define project metadata, including dependencies, scripts, versioning information, and other project-related details.
11
How can you make HTTP requests in Node.js?Ans.Node.js provides the http module for making HTTP requests. You can use the http.request() method to send HTTP requests to remote servers and handle the responses.
12
What is the purpose of the "cluster" module in Node.js?Ans.The "cluster" module in Node.js allows you to create child processes, known as workers, to handle incoming requests. It helps in utilizing multiple CPU cores and achieving better performance and scalability.
13
How does Node.js handle child processes?Ans.Node.js provides the child_process module to create and interact with child processes. It allows executing external system commands, running scripts, and communicating with child processes using IPC channels.