Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

8667 bites

Page 294

Node.js & Express2 min read

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.

Node.js & Express2 min read

__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.

Node.js & Express2 min read

Node.js File I/O: Synchronous vs. Asynchronous

Synchronous file I/O blocks your app, like waiting at a counter for your order. Asynchronous I/O gets a buzzer, letting your app work on other tasks. Use async for servers and sync for simple, one-off scripts. The footgun is using sync I/O in a server.

Node.js & Express2 min read

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
Node.js & Express2 min read

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
Node.js & Express2 min read

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
Node.js & Express2 min read

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
Node.js & Express2 min read

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
Node.js & Express2 min read

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
Node.js & Express2 min read

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.

Node.js & Express2 min read

Node.js util.promisify: From Callbacks to Promises

util.promisify converts callback-based functions into Promise-based ones, letting you use async/await with older Node.js APIs. It's a bridge for legacy code following the standard (err, value) callback pattern. The footgun: it fails on non-standard signatures.

Promise .catch(): Handling Rejections
Node.js & Express2 min read

Promise .catch(): Handling Rejections

.catch() is the try...catch for promises, intercepting errors (rejections) in a chain. Use it at the end of a promise chain to handle failures from preceding steps, like a failed API call. The footgun: placing it mid-chain can swallow errors.

Promise.then(): Each Call Returns a New Promise
Node.js & Express2 min read

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
Node.js & Express2 min read

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.

NPM Scopes: Namespacing Packages to Avoid Collisions
Node.js & Express2 min read

NPM Scopes: Namespacing Packages to Avoid Collisions

NPM scopes act like a personal folder for your packages, using the @scope/package format to avoid name collisions. They are essential for publishing private packages for your team or grouping related public ones.

Node.js & Express2 min read

Node.js Circular Dependencies: The Unfinished Export

When module A requires B, and B requires A, Node.js avoids an infinite loop by returning an unfinished version of one module's exports. This happens in complex apps with tightly coupled modules. The code doesn't crash; it fails later with a TypeError.

Structuring Express Apps with Layered Architecture
Node.js & Express2 min read

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.

Environment Variables: Config Outside Your Code
Node.js & Express2 min read

Environment Variables: Config Outside Your Code

Think of a .env file as a Post-it note of secrets for your app, kept separate from your codebase. Use it for API keys or database URLs that change between environments. The biggest mistake is committing your .env file to Git, exposing all your secrets.

Node.js & Express2 min read

ES Modules in Node.js: The Modern `import` System

ES Modules bring the browser's import/export syntax to Node.js, replacing the classic require(). Use ESM for modern projects to get features like top-level await. The main footgun is that you can't use require(), __dirname, or __filename.

npm Scripts: Your Project's Command-Line Shortcuts
Node.js & Express2 min read

npm Scripts: Your Project's Command-Line Shortcuts

Think of npm scripts as aliases for your project's common command-line tasks, stored in package.json. Use them to run build tools, start servers, or execute tests. The main footgun is forgetting npm run for custom scripts like lint or build.