Node.js interview questions
Show answer
Node runs your JavaScript on one main thread; a long computation blocks every other request until it finishes. Options: move work to worker threads, a background job queue, or a separate service; break work into chunks; or use native/streaming APIs. Keep request handlers doing I/O, not heavy computation.
Show answer
Use streams when data is large or arrives over time — file uploads, CSV exports, proxying responses — so you process it in chunks instead of loading it all into memory.
pipeline()connects streams and handles errors and backpressure (pausing a fast producer when the consumer is slow).Show answer
Read them from environment variables or a secrets manager, never hard-code or commit them. Keep a
.env.examplewith names only, add.envto.gitignore, validate required variables at start-up, and never send server-only keys to the browser. Rotate a key immediately if it leaks.Show answer
Wrap awaited code in try/catch (or use a helper that forwards rejected promises to the error middleware), return a clear status code and message without leaking stack traces, and log the details server-side. Distinguish expected errors (validation, 404) from unexpected ones (500), and add a process-level handler for unhandled rejections.