Master Nodejs Runtime with 50 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.
Node.js is a JavaScript runtime built on Chrome's V8 engine. It allows running JavaScript outside the browser, using an event-driven, non-blocking I/O model that makes it lightweight and efficient for building scalable network applications.
The event loop is the mechanism that allows Node.js to perform non-blocking I/O operations despite JavaScript being single-threaded. It continuously checks the call stack and callback queue, executing callbacks when the stack is empty. Phases include: timers → pending callbacks → idle/prepare → poll → check → close callbacks.
Timers – executes setTimeout and setInterval callbacksPending callbacks – I/O callbacks deferred to next iterationPoll – retrieves new I/O eventsCheck – executes setImmediate callbacksClose callbacks – e.g. socket.on('close')
setImmediate() executes in the check phase of the event loop, after the poll phase completes. setTimeout(fn, 0) executes in the timers phase on the next iteration. When called within an I/O callback, setImmediate() always fires before setTimeout().
process.nextTick() fires immediately after the current operation, before the event loop continues. It is processed on the microtask queue, not part of any event loop phase. setImmediate() fires on the next iteration of the event loop. Overusing nextTick can starve I/O.
Streams are objects that let you read or write data continuously in chunks rather than loading everything into memory. There are four types:Readable – e.g. fs.createReadStream()Writable – e.g. fs.createWriteStream()Duplex – both readable and writable (e.g. TCP socket)Transform – modifies data as it passes through (e.g. zlib.createGzip())
Use the .pipe() method to connect a readable stream to a writable stream:const fs = require('fs');
fs.createReadStream('input.txt')
.pipe(fs.createWriteStream('output.txt'));
The modern alternative is pipeline() from stream/promises, which handles errors automatically.
Backpressure occurs when a writable stream cannot consume data as fast as a readable stream produces it. Node.js handles this by returning false from writable.write(), signaling the readable stream to pause. The 'drain' event on the writable stream indicates it's ready for more data.
CommonJS is Node.js's original module system. Modules are loaded synchronously using require() and exported using module.exports or exports:const fs = require('fs');
module.exports = { myFunc };
Each file is treated as a separate module with its own scope.
ESM is the official JavaScript module standard, supported in Node.js 12+. Uses import/export syntax:import fs from 'fs';
export function myFunc() {}
Enable via "type": "module" in package.json or use .mjs file extension. ESM is asynchronous and supports top-level await.
Loading: CommonJS is synchronous; ESM is asynchronousSyntax: require/module.exports vs import/exportScope: CommonJS wraps in a function; ESM has strict mode by defaultTop-level await: Only ESM supports itCaching: Both cache modules, but ESM uses live bindings
Middleware functions are functions that have access to the request object (req), response object (res), and the next function (next). They can:Execute codeModify req and resEnd the request-response cycleCall next() to pass control to the next middleware
Flashcards
Flip to reveal
Focus Mode
Spaced repetition
Multiple Choice
Test your knowledge
Type Answer
Active recall
Learn Mode
Multi-round mastery
Match Game
Memory challenge