What is Node.js?
Node.js is a server-side JavaScript runtime environment built on Chrome’s V8 engine that allows executing JavaScript code outside of a web browser. It uses an event-driven, non-blocking I/O model, making it lightweight and efficient for building scalable network applications.
What is the difference between Node.js and JavaScript?
JavaScript is a programming language, while Node.js is a runtime environment that executes JavaScript on the server-side. JavaScript runs in browsers and manipulates the DOM, whereas Node.js runs on servers and provides APIs for file system operations, networking, and other server-side tasks.
Why is Node.js single-threaded?
Node.js is single-threaded to simplify development and avoid complexities of multi-threaded programming like race conditions and deadlocks. It handles concurrency through its event loop and asynchronous operations, allowing it to handle many concurrent connections efficiently without creating multiple threads.
What is the Event Loop in Node.js?
The Event Loop is the mechanism that handles asynchronous callbacks in Node.js. It continuously checks the call stack and callback queue, executing callbacks when the call stack is empty. This enables Node.js to perform non-blocking I/O operations despite being single-threaded.
Explain the difference between synchronous and asynchronous programming.
Synchronous programming executes code sequentially, blocking execution until each operation completes. Asynchronous programming allows operations to run in the background, enabling the program to continue executing other code without waiting for the operation to complete.
What is event-driven programming in Node.js?
Event-driven programming is a paradigm where the flow of the program is determined by events such as user actions, sensor outputs, or messages. In Node.js, events trigger callback functions, allowing the application to respond to multiple events asynchronously without blocking execution.
What is EventEmitter in Node.js?
EventEmitter is a class in Node.js that provides the ability to emit named events and register listener functions to respond to those events. Any object that emits events is an instance of EventEmitter, and when an event is triggered, all attached listeners execute synchronously.
What is callback hell and how can you avoid it?
Callback hell (pyramid of doom) occurs when multiple nested callbacks make code difficult to read and maintain. It can be avoided by using Promises, async/await, modularizing code into named functions, or using control flow libraries.
What are Promises in Node.js?
Promises are objects representing the eventual completion or failure of an asynchronous operation. They have three states: pending, fulfilled, or rejected. Promises allow chaining with .then() for success and .catch() for errors, making asynchronous code more manageable.
How do you use async/await in Node.js?
Async/await is syntactic sugar over Promises that makes asynchronous code look synchronous. The ‘async’ keyword is added before a function, and ‘await’ pauses execution until the Promise resolves. Use try/catch blocks for error handling with async/await.
What is the difference between process.nextTick() and setImmediate()?
process.nextTick() executes callbacks in the same phase of the event loop before moving to the next phase. setImmediate() executes callbacks in the next iteration of the event loop. process.nextTick() has higher priority and executes before I/O operations.
What is the difference between setImmediate() and setTimeout()?
setTimeout() schedules code execution after a minimum delay in milliseconds. setImmediate() schedules code execution immediately after the current event loop iteration completes. setImmediate() has higher priority in I/O cycles.
What is a Buffer in Node.js?
Buffer is a class that handles binary data directly. It’s similar to an array of integers but corresponds to raw memory allocation outside the V8 heap. Buffers are used when dealing with TCP streams, file system operations, and other binary data operations.
What are streams in Node.js?
Streams are objects that enable reading or writing data in chunks rather than loading everything into memory. There are four types: Readable (reading data), Writable (writing data), Duplex (both reading and writing), and Transform (modify data while reading/writing).
What is Express.js and how does it differ from Node.js?
Express.js is a minimal and flexible web application framework built on top of Node.js. While Node.js provides the runtime environment, Express.js provides features like routing, middleware support, template engines, and simplified HTTP utility methods for building web applications.
What is middleware in Express.js?
Middleware functions have access to request (req), response (res), and next() objects. They execute during the request-response cycle, can modify req/res objects, end the cycle, or call the next middleware. Examples include authentication, logging, and body parsing.
What are the types of middleware in Express.js?
There are five types: Application-level middleware (app.use()), Router-level middleware (router.use()), Error-handling middleware (4 parameters), Built-in middleware (express.json(), express.static()), and Third-party middleware (body-parser, cors, helmet).
What is the role of next() in middleware?
next() is a callback function that passes control to the next middleware in the stack. If next() is not called, the request hangs and never proceeds. Calling next(error) skips to error-handling middleware.
What is package.json?
package.json is a manifest file that contains metadata about the project including name, version, dependencies, scripts, and configuration. It’s used by npm to manage project dependencies and provides information about the application.
What is the difference between dependencies and devDependencies?
dependencies contain packages required for the application to run in production. devDependencies contain packages needed only during development and testing (like testing frameworks, build tools). Production builds typically exclude devDependencies.
What is the purpose of module.exports?
module.exports is used to export functions, objects, or values from a module so they can be imported and used in other files using require(). It enables code modularization and reusability across the application.
What is the difference between module.exports and exports?
module.exports is the actual object that gets returned when require() is called. exports is a reference to module.exports. Assigning a new value to exports breaks the reference, so module.exports should be used for exporting new objects or functions.
What is npm?
npm (Node Package Manager) is the default package manager for Node.js. It allows developers to install, share, and manage dependencies. It provides access to the world’s largest software registry with millions of reusable packages.
Name five built-in modules in Node.js.
1) fs (file system operations), 2) http (creating HTTP servers), 3) path (file path utilities), 4) os (operating system information), 5) events (EventEmitter class for event handling).