-
What is Node.js?
- Node.js is a JavaScript runtime environment that runs JavaScript code outside the browser.
- Built on Chrome’s V8 engine for fast execution.
- Allows building server-side and backend applications using JavaScript.
- Supports non-blocking, event-driven architecture for handling multiple requests efficiently.
- Comes with npm (Node Package Manager) to manage packages and dependencies.
Tip: Node.js lets you use JavaScript for both frontend and backend development.
-
Why Node is single-threaded?
- Node.js uses a single-threaded event loop to handle requests.
- It is designed to be non-blocking and asynchronous, so one thread can manage many connections efficiently.
- Heavy I/O operations don’t block the thread; they use callbacks, promises, or async/await.
- Single-threaded design reduces context switching and memory overhead.
Tip: Node’s single-threaded model is ideal for I/O-heavy applications, not CPU-intensive tasks.
-
Event loop in Node
- The event loop is Node.js’s mechanism to handle asynchronous operations.
- It allows Node to perform non-blocking I/O using a single thread.
- Works in phases: timers, I/O callbacks, idle/prepare, poll, check, close callbacks.
- Executes callbacks from callbacks queue when the main thread is free.
- Enables Node to handle thousands of concurrent connections efficiently.
Tip: Understanding the event loop is key to writing efficient asynchronous Node.js applications.
-
Non-blocking I/O
- Non-blocking I/O allows Node.js to initiate I/O operations (like reading files or network requests) without waiting for them to complete.
- Other code continues executing while I/O is processed asynchronously.
- Uses callbacks, promises, or async/await to handle results once ready.
- Improves performance by handling multiple requests concurrently on a single thread.
Tip: Non-blocking I/O is what makes Node.js fast and scalable for I/O-heavy applications.
-
What is npm?
- npm (Node Package Manager) is the default package manager for Node.js.
- Used to install, manage, and share JavaScript packages and libraries.
- Allows version control of dependencies via
package.json.
- Provides a vast ecosystem of open-source modules.
- Supports scripts for running tasks like build, test, or start.
Tip: npm simplifies dependency management and accelerates development with reusable packages.
-
package.json
package.json is a manifest file for a Node.js project.
- Stores project metadata: name, version, description, author, license.
- Lists dependencies and devDependencies needed for the project.
- Defines scripts like
start, build, test to automate tasks.
- Helps npm install, update, and manage packages consistently.
Tip: Always keep package.json updated to ensure project reproducibility and dependency management.
-
What is module in Node?
- A module in Node.js is a reusable block of code encapsulated in a file.
- Provides functionality that can be exported and imported using
module.exports or exports.
- Can be built-in (core), third-party (npm), or custom modules.
- Helps organize code into smaller, maintainable pieces.
Tip: Modules make Node.js applications modular, reusable, and easier to maintain.
-
CommonJS
- CommonJS is a module system used in Node.js.
- Uses
require() to import modules and module.exports to export them.
- Synchronous loading of modules, suitable for server-side environments.
- Enables modular and reusable code structure.
- Standard for most Node.js core and npm packages.
Tip: CommonJS is the default module system in Node.js, though ES Modules (import/export) are also supported.
-
What is require?
require() is a Node.js function used to import modules into a file.
- Loads core, third-party, or custom modules.
- Returns the
module.exports object from the required module.
- Synchronous operation: code waits until the module is loaded.
- Example:
const fs = require('fs'); // import Node.js file system module
Tip: Use require() to organize code by reusing modules efficiently in Node.js.
-
What is fs module?
fs (File System) is a built-in Node.js module for working with files and directories.
- Allows reading, writing, updating, deleting, and watching files.
- Supports synchronous and asynchronous methods.
- Examples of methods:
fs.readFile(), fs.writeFile(), fs.appendFile(), fs.unlink().
Tip: Use asynchronous methods in fs to avoid blocking the Node.js event loop.
-
What is path module?
path is a built-in Node.js module for working with file and directory paths.
- Provides utilities to join, resolve, normalize, and parse paths across different OS.
- Common methods:
path.join(), path.resolve(), path.basename(), path.extname().
- Helps avoid errors with file separators (
/ vs \\) across platforms.
Tip: Use path to handle file paths safely and consistently in Node.js projects.
-
What is http module?
http is a built-in Node.js module used to create HTTP servers and clients.
- Allows handling requests and sending responses over the web.
- Common methods:
http.createServer(), server.listen(), req.on(), res.write(), res.end().
- Forms the foundation for building web servers without additional frameworks.
Tip: Use http for lightweight servers; for complex apps, combine with frameworks like Express.js.
-
How Node handles async tasks
-
What is process?
-
What is env variable?
-
What is cluster?
-
What is child process?
-
What is buffer?
-
What is stream?
-
Readable vs writable stream