NodeJS Answers
0:000:00
Node.js Answers
Basic Node.js Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is Node.js? | A JavaScript runtime environment built on Chrome's V8 JavaScript engine | Allows running JavaScript code outside of a web browser |
2 | What is the purpose of Node.js? | Building server-side applications, network tools, command-line tools | Creating web servers, APIs, real-time applications, microservices |
3 | What is NPM? | Node Package Manager; the default package manager for Node.js | Installing, managing, and sharing Node.js packages (libraries) |
4 | How do you install a package using NPM? | Using npm install | npm install express |
5 | What is the package.json file? | A manifest file for a Node.js project, containing metadata and dependencies | Defines project dependencies, scripts, version |
6 | What is a module in Node.js? | A JavaScript file containing code that can be imported and exported | fs, http, path are built-in modules |
7 | How do you import a module? | Using the require() function | const fs = require('fs');, const myModule = require('./my-module'); |
8 | How do you export a module? | Using module.exports or exports | module.exports = { myFunction: ... };, exports.myVar = ...; |
9 | What is Express.js? | A popular minimal and flexible Node.js web application framework | const express = require('express'); const app = express(); |
10 | What is the Event Loop? | The core mechanism in Node.js that handles asynchronous operations | Manages callbacks, timers, I/O operations, etc. |
11 | What is asynchronous programming in Node.js? | Writing code that performs operations without blocking the main thread | Using callbacks, Promises, or async/await |
12 | What is a callback function? | A function passed as an argument to another function to be executed later | fs.readFile('file.txt', (err, data) => { ... }); |
13 | What is the purpose of the process object? | Provides information about and control over the current Node.js process | process.env, process.argv, process.exit() |
14 | What is the fs module? | Provides functions for interacting with the file system | fs.readFile(), fs.writeFile(), fs.mkdir() |
15 | What is the http module? | Provides functionality for creating HTTP servers and clients | http.createServer((req, res) => { ... }); |
Intermediate Node.js Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is middleware in Express.js? | Functions that have access to the request object (req), response object (res), and the next middleware function in the application's request-response cycle | app.use((req, res, next) => { console.log('Request received'); next(); }); |
2 | How do you handle routing in Express.js? | Using app.get(), app.post(), app.put(), app.delete(), etc. | app.get('/', (req, res) => { res.send('Home'); }); |
3 | What are route parameters? | Placeholders in a route path that capture values | app.get('/users/:id', (req, res) => { res.send(req.params.id); }); |
4 | What are query parameters? | Key-value pairs appended to the URL after a ? | app.get('/search', (req, res) => { res.send(req.query.q); }); |
5 | What is a Promise? | An object representing the eventual completion (or failure) of an asynchronous operation | new Promise((resolve, reject) => { ... }).then(result => ...).catch(error => ...); |
6 | What is async/await? | Syntax sugar over Promises, making asynchronous code look and feel more synchronous | async function fetchData() { const response = await fetch('/api'); ... } |
7 | What is the EventEmitter class? | A base class for objects that can emit and handle events | const EventEmitter = require('events'); const myEmitter = new EventEmitter(); myEmitter.on('event', () => { ... }); |
8 | What are Streams? | A way to handle data in chunks, rather than loading the entire data into memory | fs.createReadStream('file.txt').pipe(fs.createWriteStream('new.txt')); |
9 | Explain the concept of error handling in Node.js. | Using try...catch blocks (for synchronous code), .catch() (for Promises), error handling middleware | try { ... } catch (err) { ... }, promise.catch(err), app.use((err, req, res, next) => { ... }); |
10 | What are environment variables? | Variables that store configuration information specific to the deployment environment | process.env.PORT, process.env.DB_HOST |
11 | What is nodemon? | A utility that monitors changes in the source code and automatically restarts the Node.js app | npm install --save-dev nodemon, then nodemon server.js |
12 | How do you manage dependencies? | Using the package.json file and npm install | npm install express lodash |
13 | What is the difference between require() and import? | require() is CommonJS (Node.js default); import is ES Modules (modern standard) | const express = require('express'); vs import express from 'express'; |
14 | What is CORS (Cross-Origin Resource Sharing)? | A mechanism that allows a web page to request resources from a different domain (origin) | Using cors middleware in Express: app.use(cors()); |
15 | What are common HTTP methods? | GET, POST, PUT, DELETE, PATCH | GET for fetching, POST for creating, PUT for updating, DELETE for removing. |
Advanced Node.js Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | Explain the Event Loop in more detail. | Phases: Timers, Pending Callbacks, Idle/Prepare, Poll, Check, Close Callbacks | Understanding how Node.js handles asynchronous operations and manages the event queue. |
2 | What is the cluster module? | Allows creating multiple Node.js processes that share a single server port, utilizing multi-core CPUs | cluster.isMaster, cluster.isWorker, cluster.fork() |
3 | What is the purpose of a microservices architecture? | Breaking down a monolithic application into smaller, independent services | Scalability, resilience, maintainability, independent deployment. |
4 | What is an ORM (Object-Relational Mapper)? | N/ODM (Object-Document Mapper); Examples: Sequelize, Mongoose (for MongoDB) | const User = mongoose.model('User', new Schema({...})); |
5 | What are Worker Threads? | Allows running JavaScript code in separate threads for CPU-intensive tasks without blocking the main event loop | const worker = new Worker('./worker.js'); |
6 | What is the concept of dependency injection? | A design pattern where dependencies are provided to a component rather than being created internally | Using constructor parameters, setter methods, or interfaces to inject dependencies. |
7 | How do you handle testing in Node.js? | Using frameworks like Jest, Mocha, Chai, Supertest | Unit tests, integration tests, end-to-end tests. |
8 | What is the purpose of error handling middleware in Express.js? | A special middleware function with four arguments ((err, req, res, next)) to catch errors | app.use((err, req, res, next) => { ... }); |
9 | What is rate limiting? | Restricting the number of requests a client can make to an API within a specific time frame | Using middleware libraries like express-rate-limit to prevent abuse. |
10 | What is input validation? | Ensuring incoming data meets specific format and type requirements | Using libraries like Joi, Zod, express-validator |
11 | What is the concept of caching in Node.js? | Storing frequently accessed data in a faster store (e.g., Redis, Memcached) to improve performance | Using libraries like node-cache or integrating with Redis. |
12 | What is the purpose of WebSockets? | Enables real-time, bidirectional communication between client and server over a single connection | Real-time chat applications, live updates (e.g., using Socket.IO). |
13 | What is the concept of GraphQL? | A query language for APIs that allows clients to request exactly the data they need | Using Apollo Server, Express-GraphQL |
14 | What is a message queue? | A system for asynchronous communication between application components (e.g., RabbitMQ, Kafka) | Using libraries like bull, amqplib |
15 | How do you handle authentication and authorization? | Verifying a user's identity (authentication) and determining what they are allowed to do (authorization) | Using JWT (JSON Web Tokens), OAuth, session-based authentication. |