Good Code
The good version awaits an async file read and lets the event loop continue accepting other request work while the disk is busy.
Lesson 02
Use async filesystem APIs in request paths so one slow disk read does not block the event loop.
import { readFile } from "node:fs/promises";
import type { ServerResponse } from "node:http";
export async function sendPublicConfig(response: ServerResponse) {
// Async I/O lets the event loop keep serving other requests.
const file = await readFile("public-config.json", "utf8");
response.setHeader("content-type", "application/json");
response.end(file);
}import { readFileSync } from "node:fs";
import type { ServerResponse } from "node:http";
export function sendPublicConfig(response: ServerResponse) {
// Sync I/O blocks the whole process during a request.
const file = readFileSync("public-config.json", "utf8");
response.setHeader("content-type", "application/json");
response.end(file);
}The good version awaits an async file read and lets the event loop continue accepting other request work while the disk is busy.
The bad version uses readFileSync in a request path, blocking every other request in the process until the read finishes.