NodeJS Interview Questions and Answers
What is Node.js?
- Node.js is an open-source, cross-platform JavaScript runtime built on Chrome's V8 JavaScript engine. It allows developers to build scalable, high-performance applications, particularly for server-side scripting.
What are the key features of Node.js?
- Asynchronous and event-driven architecture
- Single-threaded event loop
- Non-blocking I/O
- Fast execution due to V8 engine
- Cross-platform compatibility
What is the difference between Node.js and JavaScript?
- JavaScript is a programming language primarily used for client-side scripting in browsers. Node.js, on the other hand, is a runtime environment that allows JavaScript to be used for server-side programming.
What is npm in Node.js?
- npm (Node Package Manager) is a package manager for Node.js. It is used to install and manage third-party packages, libraries, and dependencies in Node.js projects.
What is an event loop in Node.js?
- The event loop is the core mechanism that allows Node.js to perform non-blocking, asynchronous operations. It continuously checks the event queue for pending tasks and processes them one by one without blocking the main thread.
What is the purpose of `require()` in Node.js?
- `require()` is used to import modules in Node.js. It loads and caches the module and makes it available for use in the current file.
What are streams in Node.js?
- Streams are objects in Node.js used to handle reading/writing of data in chunks. They are particularly useful for handling large amounts of data, such as file or network operations, in a memory-efficient manner.
What are the different types of streams in Node.js?
- Readable streams: For reading data.
- Writable streams: For writing data.
- Duplex streams: Can read and write data.
- Transform streams: A type of duplex stream where the output is computed based on the input.
What is the purpose of the `fs` module in Node.js?
- The `fs` module is used for interacting with the file system. It provides methods to read, write, update, and delete files, as well as handle directories and file metadata.
What are callbacks in Node.js?
- Callbacks are functions that are passed as arguments to other functions and are invoked when the operation completes. They are commonly used in Node.js for asynchronous operations.
What is middleware in Node.js?
- Middleware is a function in a Node.js web application (e.g., Express.js) that is executed during the request-response cycle. It can modify request and response objects, execute code, or end the request-response cycle.
What is Express.js?
- Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It simplifies routing, middleware handling, and HTTP requests.
What are the different HTTP methods in Node.js?
- GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD
What is an HTTP request in Node.js?
- An HTTP request is made by the client to the server to fetch data or interact with a server-side resource. It includes methods (GET, POST, etc.), headers, and sometimes a body (e.g., for POST requests).
What is an HTTP response in Node.js?
- An HTTP response is the data returned by the server in response to an HTTP request. It includes a status code, headers, and possibly a body with the requested content or error messages.
What is the `process` object in Node.js?
- The `process` object provides information and control over the current Node.js process, such as command-line arguments, environment variables, and methods for interacting with the system's input/output.
What is the `Buffer` class in Node.js?
- The `Buffer` class is used to handle binary data in Node.js. It provides methods to read and write data to memory buffers directly, which is useful for working with streams and file operations.
What is the difference between synchronous and asynchronous code in Node.js?
- Synchronous code is executed line by line, blocking the execution of subsequent code until the current operation is complete. Asynchronous code allows operations to run independently, enabling the application to perform other tasks while waiting for the operation to complete.
What is the `eventEmitter` class in Node.js?
- The `EventEmitter` class is a core class in Node.js that allows an object to emit events and register listeners for those events. It enables communication between different parts of an application through events.
What is the purpose of the `cluster` module in Node.js?
- The `cluster` module allows Node.js to create multiple child processes (workers) to handle multiple CPU cores. This is useful for improving the performance and scalability of Node.js applications in multi-core environments.
What are the common error-handling methods in Node.js?
- Callbacks (with error as the first argument), Promises, and try-catch blocks are commonly used to handle errors in Node.js.
What is a Promise in Node.js?
- A Promise is an object representing the eventual completion or failure of an asynchronous operation. It provides a cleaner and more manageable way to work with asynchronous code, especially for chaining multiple operations.
What is the purpose of the `async` and `await` keywords in Node.js?
- The `async` keyword defines a function that returns a Promise, and the `await` keyword pauses the execution of an `async` function until the Promise is resolved, providing a more synchronous way of working with asynchronous code.
What is the difference between `require` and `import` in Node.js?
- `require` is used in CommonJS modules and allows for importing modules. `import` is used in ECMAScript modules and is a more modern way to import modules, supporting static analysis and tree shaking.
What is CORS in Node.js?
- CORS (Cross-Origin Resource Sharing) is a security feature that allows or restricts web pages from making requests to a domain other than their own. It is configured on the server to allow specific origins or methods.
What is the purpose of `socket.io` in Node.js?
- `socket.io` is a library that enables real-time, bi-directional communication between clients and servers over WebSockets or long polling. It is widely used for building chat applications and real-time collaboration features.
What is the difference between Node.js and PHP?
- Node.js is a server-side JavaScript runtime environment, while PHP is a server-side scripting language. Node.js is non-blocking and event-driven, whereas PHP is typically blocking and synchronous.
What are environment variables in Node.js?
- Environment variables are key-value pairs that provide configuration settings for applications. In Node.js, they are accessed through `process.env` and are commonly used for managing secrets, configurations, and deployment settings.
What is `npm start` used for?
- `npm start` is a command used to start the application. It runs the script defined under "start" in the `package.json` file, often used for launching server applications.
What is the `path` module in Node.js?
- The `path` module provides utilities for working with file and directory paths, such as joining paths, resolving relative paths, and extracting file extensions or names.
What is a `404` error in Node.js?
- A `404` error is an HTTP status code that indicates the requested resource could not be found on the server. It is often returned when a user tries to access a non-existent URL.
What is the difference between `npm install` and `npm update`?
- `npm install` installs the dependencies listed in the `package.json` file, while `npm update` updates the installed dependencies to their latest compatible versions.
What is a WebSocket in Node.js?
- WebSockets provide full-duplex communication channels over a single TCP connection. In Node.js, they are often used for real-time applications, allowing bidirectional communication between the server and clients.
What is the use of `request` module in Node.js?
- The `request` module is a simplified HTTP client for making HTTP requests to external APIs and web services. It is often used for interacting with RESTful APIs in Node.js applications.