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.

4330 bites

Page 70

Node.js & Express1 min read

Promise.all vs Promise.allSettled

All rejects on the first failure; allSettled always fulfills with a status/value or reason per input. Use allSettled when partial success is acceptable.

Node.js & Express1 min read

Async iterators and for await...of for streaming

Async iterators yield values lazily over time; for await...of consumes them sequentially with backpressure, keeping memory bounded.

Node.js & Express1 min read

fs.readFileSync vs fs.readFile

Sync blocks the event loop and returns directly, async takes a callback or promise, only sync is safe at startup.

Node.js & Express1 min read

Why use path.join over string concatenation

Path.join uses the correct OS separator, collapses duplicate slashes, normalizes . and .. segments.

Node.js & Express1 min read

Minimal HTTP server with the http module

CreateServer with a request listener, set status and Content-Type, end the response, call listen on 3000.

Node.js & Express1 min read

Counting lines in a 5GB log file efficiently

ReadFile loads all 5GB into RAM and may exceed buffer limits, instead stream with createReadStream plus readline and count line by line.

Node.js & Express1 min read

Reading a POST body from the request stream

Body arrives in chunks via data events, accumulate them, on the end event concatenate and JSON.parse inside try/catch.

Node.js & Express1 min read

path.resolve vs path.join

Join concatenates and normalizes relative segments, resolve builds an absolute path right to left from cwd and resets on any absolute segment.

Node.js & Express1 min read

fs.watch vs fs.watchFile

Watch uses OS event notifications, efficient but inconsistent across platforms, watchFile polls stat at an interval, reliable but slower.

Node.js & Express1 min read

Scaling across cores with cluster and os

Os.cpus gives core count, the primary forks one worker per core, all workers share the listening port, the OS load-balances connections.

Node.js & Express1 min read

Backpressure in Node.js streams

Backpressure pauses the readable when the writable buffer fills, pipe and pipeline manage it automatically, pipeline also propagates errors and cleans up.

Node.js & Express1 min read

http.Agent and connection pooling

The agent pools and keeps sockets alive, avoiding repeated TCP and TLS handshakes, controlled by keepAlive and maxSockets.

Node.js & Express1 min read

Minimal Express Hello World server

Import express, create an app, define app.get on the root sending a response, call app.listen on a port.

Node.js & Express1 min read

Serving static files in Express

App.use with express.static pointing at the public directory, files served relative to that root, often combined with an absolute path.

Node.js & Express1 min read

Query params vs route params in Express

Query string values via req.query.q, named path segments via req.params.id, query is optional, route params are part of the matched pattern.

Node.js & Express1 min read

Parsing JSON bodies with express.json

Register express.json via app.use so it reads the request stream and populates req.body before handlers run, place it before routes.

Node.js & Express1 min read

res.send vs res.json vs res.end

Send is flexible and sets content type by type, json serializes and sets JSON content type, end is the raw http terminator with no body helpers.

Node.js & Express1 min read

Express route order and matching

Express checks routes in definition order, /items/new matches the literal route first, if /:id came first new would be captured as an id.

Node.js & Express1 min read

Modularizing routes with express.Router

Create a Router instance in users.js, attach routes to it, export it, then mount it under a base path with app.use in the main file.

Node.js & Express1 min read

Custom API key auth middleware

Read the header from req, on missing or invalid send res.status(401) and return, on valid call next, mount before protected routes.