Node.js leans on a handful of design patterns that fit its module system, async model, and streaming I/O. The most common ones every backend developer should recognize:
Node.js leans on a handful of design patterns that fit its module system, async model, and streaming I/O. The most common ones every backend developer should recognize:
module.exports / export, keeping internals private. The foundation of code organization in Node.// db.js — every `require("./db")` gets this same pool (singleton via module cache).
const pool = createPool(process.env.DATABASE_URL);
module.exports = pool;
function createLogger(kind) {
return kind === "json" ? new JsonLogger() : new TextLogger();
}
EventEmitter, streams, and most core modules use it.const { EventEmitter } = require("node:events");
const bus = new EventEmitter();
bus.on("order:paid", (o) => sendReceipt(o));
bus.emit("order:paid", order);
(ctx, next), popularized by Express/Koa. Great for cross-cutting concerns (auth, logging, validation).app.use((req, res, next) => { req.startedAt = Date.now(); next(); });
const { pipeline } = require("node:stream/promises");
await pipeline(fs.createReadStream("in"), gzip(), fs.createWriteStream("out.gz"));
| Need | Pattern |
|---|---|
| One shared resource | Singleton (module cache) |
Build objects, hide new | Factory |
| React to async events | Observer / EventEmitter |
| Cross-cutting request logic | Middleware |
| Process big data safely | Streams / pipeline |
| Testable, decoupled deps | Dependency Injection |
Patterns are a shared vocabulary: saying "the config is a module-cached singleton" or "auth is middleware" communicates a design in a few words. Node's own core (EventEmitter, streams, the module system) is built from these, so recognizing them helps you read frameworks and structure your own services. Interviewers use this question to check that you don't just wire callbacks ad hoc, but reach for the pattern that fits — and know its trade-offs, like a singleton making global state harder to test.
A library of IT interview questions with detailed answers — from Junior to Senior.
Donate