CodeOath
← All posts
Node.js36 min total · 14 parts

Node.js Fundamentals: The Runtime, the Event Loop, and Building Real APIs

Part 14 of 14 · ~2 min

Common Mistakes Worth Remembering

  • Assuming Node's single-threaded JS execution means it can't handle concurrency well — it handles I/O concurrency extremely well; what it can't do is run two pieces of your JS at the exact same instant.
  • Blocking the one JS thread with synchronous CPU-heavy work inside a request handler, freezing every other in-flight request on that process, not just the slow one.
  • Treating process.nextTick as equivalent to a Promise microtask — it always drains first, and an unbounded chain of nextTick calls starves the event loop even more thoroughly than an unbounded microtask chain does.
  • Firing an async call without await or a .catch() and assuming its eventual rejection will surface somewhere useful on its own.
  • Forgetting that a .js file's module system (CommonJS vs. ESM) is decided by the nearest package.json's "type" field, not by anything in the file itself.
  • Reading an entire large file into memory with fs.readFile when a stream would serve it incrementally with roughly constant memory use.
  • Assuming .env files load automatically — Node doesn't read them without dotenv (or an explicit --env-file flag), and the loading call has to run before anything that reads the resulting process.env values.
  • Treating a programmer error (a genuine bug) the same way as an operational error (expected, recoverable failure) — swallowing a bug and continuing risks running further code against state the bug has already corrupted.
  • Opening a fresh database connection per request instead of using a pool, exhausting the database's connection limit under real concurrent load.
  • Deploying with bare node server.js and no process manager, so a single uncaught exception takes the whole service down with no automatic restart.
  • Ignoring SIGTERM, so every deploy or scale-down drops whatever requests happen to be in flight at that exact moment.

Node's event-loop model is the same fundamental idea as the browser event loop covered in the JavaScript reference, extended with real OS-level I/O and a phase-based macrotask layer instead of a single generic task queue — and Express's middleware pipeline is the same onion-shaped pattern covered across three other frameworks in Middleware Pipelines Compared. Try the event-loop ordering examples and the streaming server above as runnable code in the code lab.