10LOC

#http

Node.jsintermediate

Server-Sent Events (SSE) with plain res.write, no socket.io

import { createServer, type ServerResponse } from "node:http";

const clients = new Set<ServerResponse>();

const server = createServer((req, res) => {

A live event stream over plain HTTP using res.write and text/event-stream — no WebSocket library, no socket.io, and free auto-reconnect in the browser.

Node.jsintermediate

Graceful shutdown: draining connections on SIGTERM

import { createServer } from "node:http";

const server = createServer((_req, res) => {
  setTimeout(() => res.end("ok"), 50); // pretend this request takes a moment
});

Stop accepting new connections on SIGTERM, let in-flight requests finish, and force-exit if draining takes too long.

Node.jsadvanced

AsyncLocalStorage for request-scoped trace IDs across async calls

import { AsyncLocalStorage } from "node:async_hooks";
import { createServer } from "node:http";
import { randomUUID } from "node:crypto";

const requestContext = new AsyncLocalStorage<string>();

Thread a trace ID through every async call of a request, without passing it as an argument through every function in between.