Latest in Snippets

Home → Snippets

Small, self-contained code snippets you can drop straight into a project — each one solving a single Node.js or JavaScript problem well.

Modern JavaScript features you can use in Node.js 26

A batch of genuinely useful JavaScript additions is now unflagged in Node.js 26: Set operations (union, intersection, difference), lazy iterator helpers, RegExp.escape, Promise.try, Array.fromAsync, and Float16Array. Here's each one with real, verified output.

Temporal: the modern way to handle dates in Node.js

JavaScript's broken Date object finally has a replacement. Temporal — Stage 4 in ES2026 and enabled by default in Node.js 26 — brings immutable date/time types, real time-zone support, and sane arithmetic. Here's how to use it, with examples and the polyfill for older environments.

Pretty-print JSON in JavaScript with JSON.stringify(): indentation, replacers, and what silently gets lost

JSON.stringify(value, null, 2) pretty-prints any object without a library. Here's what the other two arguments do, which values JSON silently drops or mangles (undefined, NaN, Map, Set, Date), which ones throw (BigInt, circular references), and the newer JSON.rawJSON() for numbers too big for JavaScript. All output is real, from Node 24 and 26.

Block IP addresses in Node.js and Express: net.BlockList, ::ffff: addresses and X-Forwarded-For spoofing

Block single IPs, ranges and CIDR subnets in a Node.js or Express server with the built-in net.BlockList, without any package. The common snippet, x-forwarded-for || remoteAddress, fails twice: it never matches because Node reports ::ffff: addresses, and any client can bypass it with one header. Here's a version that works directly and behind nginx, tested on Node 24 and 26 with Express 5.

5 ways to create an HTTP or HTTP/2 server in Node.js: node:http, Express 5, Fastify 5, Hono and node:http2

The same small JSON API built five ways: plain node:http, Express 5, Fastify 5, Hono, and an HTTP/2 server that browsers can actually reach. Testing them turned up a common route-validation bug, an Express 5 migration trap, and the reason the usual http2.createServer() snippet silently ignores your TLS certificate. Tested on Node 24 and 26 with curl and Chromium.

Stop CSS columns from splitting an element: break-inside: avoid

With CSS columns, a card or list item can get cut in half, with its top at the bottom of one column and the rest at the top of the next. One line fixes it: break-inside: avoid. Tested in Chromium, Firefox and WebKit, including what the legacy page-break-inside does today and the case it can't fix.

An accessible pure CSS toggle switch: one checkbox, appearance: none, role="switch"

A toggle switch needs no JavaScript and no extra markup: style a real checkbox with appearance: none and give it role="switch". Unlike the classic "hidden checkbox + styled label" trick, it stays reachable with the keyboard, announces itself to screen readers, works in forced-colors mode and respects reduced motion. Tested in Chromium, Firefox and WebKit.

Format bytes as kB, MB and GB in JavaScript: Intl.NumberFormat, 1000 vs 1024, and the edge cases

A small, tested formatBytes() that turns 2151099283 into "2.2 GB" (or "2 GiB"), with localised output from Intl.NumberFormat. Plus why the popular Math.log one-liner prints "NaN undefined" for 0 and "1024.00 KB" for 1,048,575 bytes, and which convention (1000 or 1024) to use.

Parse URL query parameters in JavaScript: URLSearchParams, its gotchas, and reacting to changes

URLSearchParams parses query strings in every browser and in Node.js. Here's how to read, convert and update parameters, the gotchas that cause real bugs (+ becoming a space, repeated keys, Object.fromEntries dropping values), URL.parse(), and how to react when the query string changes. Every example verified in Node 24 and 26, and in headless Chromium, Firefox and WebKit.

Create an array from 0 to N in JavaScript: every way, the gotchas, and which is fastest

[...Array(n).keys()] and Array.from({ length: n }, (_, i) => i) both give you [0, 1, …, n-1], but they behave differently for "3", 2.5 or -1, and Array(n).map() doesn't work at all. Here's what each one-liner really does (with real output), a range(start, end, step) helper, the typed-array and floating-point traps, lazy iterator helpers, and a benchmark on Node 24 and 26.

About Code with Node.js

This is a personal blog and reference point of a Node.js developer.

I write and explain how different Node and JavaScript aspects work, as well as research popular and cool packages, and of course fail time to time.