Node.js
42 bites tagged Node.js — interview questions with model answers, and 60-second explainers.
Error-First Callbacks: Node.js's Original Async Handler
The error-first callback is a Node.js convention: check for rain before unpacking the picnic. The first argument to any async callback is for an error. It's the standard for older core modules like `fs`.
API Versioning: Managing Change Without Breaking Clients
API versioning lets you evolve an API without breaking existing clients. It's essential for public APIs or services with multiple frontends that can't update in lockstep. The footgun is delaying versioning, forcing a painful migration on early users.
Helmet.js: Secure Express Apps with HTTP Headers
Helmet.js adds a security layer to Express apps by setting crucial HTTP headers. Use it in any public-facing Node app to prevent common attacks like XSS. The footgun: its default Content-Security-Policy is strict and requires app-specific configuration.
Hashing Data with Node.js's `crypto` Module
Hashing creates a unique, fixed-size fingerprint of data. It's a one-way process used to verify data integrity or store passwords securely without saving the plain text. The footgun is using weak algorithms like MD5 or SHA1 for security-sensitive tasks.
Node.js os Module: Reading Your System's Vital Signs
The Node.js `os` module is your app's dashboard for the host machine's vitals. Use it to check CPU cores, free memory, or platform type to adapt your code. The footgun is assuming consistency; system details can vary wildly between environments.
__dirname and __filename: Path Anchors in Node.js
__dirname and __filename act as GPS for your Node.js files, giving you the absolute path to the current file and its directory. This is essential for reliably loading adjacent files, like templates or configs. The main gotcha: they don't exist in ES Modules.
Creating a Basic HTTP Server in Node.js
A Node.js HTTP server is a listening post that waits for requests on a port and runs your code to reply. It's the foundation for any web service, from simple APIs to full apps. The footgun: forgetting `response.end()` leaves the client hanging indefinitely.
Top-Level Await: `await` Without an `async` Function
Top-level await lets you use `await` directly in an ES module, no `async` function needed. Use it to initialize resources like database connections on startup. The footgun: the entire module's execution blocks until the promise resolves, delaying startup.
Promise.any(): Get the Fastest Successful Result
Promise.any() is a race where only finishers count. It returns the value of the first promise to succeed, ignoring any that fail. Use it to query redundant endpoints and take the first successful response.
Promise.allSettled(): Never Fail a Batch of Promises
Promise.allSettled() waits for every promise in a set to finish, success or fail, without short-circuiting. Use it for independent tasks, like multiple API calls, where you need the outcome of each.
Promise.race(): First Promise to Settle Wins
Promise.race() returns a promise that mirrors the outcome of the first promise in a set to finish—the winner takes all, whether it resolves or rejects. Use it to set a timeout on a network request.
Promise.all(): Wait for Multiple Promises at Once
Promise.all() runs multiple promises in parallel, resolving only when all have succeeded. It's for when you need data from several API endpoints to render a single component.
Async/Await: Write Non-Blocking Code That Reads Synchronously
async/await lets you write non-blocking code that reads like simple, synchronous logic. It's used for network requests or database queries without freezing your app. The biggest footgun is using `await` inside a function you forgot to declare as `async`.
Promise.then(): Each Call Returns a New Promise
Each `.then()` call returns a new promise, letting you chain async tasks sequentially. This is key for multi-step operations like API calls. The footgun is attaching multiple `.then()`s to the original promise, which executes them in parallel, not in sequence.
JavaScript Promises: Handling Future Values
A Promise is an IOU for a future value from an async operation. Instead of blocking your code, you get an object that will eventually contain the result or an error. They're essential for API calls or file reads.
Structuring Express Apps with Layered Architecture
Think of your Express app as a three-story building: a web layer for HTTP traffic, a service layer for business logic, and a data layer for your database. This keeps code organized and testable.
Worker Threads: True Parallelism in Node.js
Worker threads give Node.js a separate brain for heavy lifting, letting you run CPU-intensive code without blocking the main event loop. Use them for tasks like image processing, not I/O. The footgun is assuming memory is shared; it isn't.
JavaScript's Event Loop: Macrotasks & Microtasks
The JavaScript event loop processes tasks like `setTimeout` callbacks or user clicks from a macrotask queue. A single, long-running macrotask blocks all rendering and user input, freezing the UI. The footgun is assuming `setTimeout(fn, 0)` runs instantly.
Get Node.js bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.