Node.js Middle interview questions and answers

75 questions for a Node.js (Middle) interview - with correct answers, explanations and links to the docs.

Take this quiz
  1. 1.Which list shows the main phases of the Node.js event loop in order?
  2. 2.What is the order of the output?
  3. 3.Inside an I/O callback (for example, in fs.readFile), which runs first?
  4. 4.Why can recursive process.nextTick calls be dangerous?
  5. 5.What does the libuv thread pool handle by default?
  6. 6.A request handler runs a CPU-heavy loop for 2 seconds. What happens to other requests?
  7. 7.Which of these can block the event loop?
  8. 8.How do you measure how busy the event loop is?
  9. 9.What does timer.unref() do?
  10. 10.What does await setTimeout(1000) imported from node:timers/promises do?
  11. 11.Why use streams to send a 2 GB file instead of readFile?
  12. 12.What are the four basic types of streams in Node.js?
  13. 13.What is backpressure in streams?
  14. 14.Why is stream.pipeline() preferred over chaining .pipe()?
  15. 15.How can you read a readable stream line by line or chunk by chunk with async/await?
  16. 16.What is a Buffer?
  17. 17.What is the difference between Buffer.alloc(10) and Buffer.allocUnsafe(10)?
  18. 18.What does Buffer.from("привіт").length return?
  19. 19.What is objectMode in streams?
  20. 20.How do you gzip a file with streams?
  21. 21.What happens if an EventEmitter emits error and there is no error listener?
  22. 22.What does the warning MaxListenersExceededWarning usually point to?
  23. 23.How do you wait for one event as a promise?
  24. 24.What is the difference between operational errors and programmer errors?
  25. 25.Why is it useful to pass cause when re-throwing an error?
  26. 26.What is AbortController used for in Node.js?
  27. 27.Why should every outgoing HTTP call have a timeout?
  28. 28.How do you define error-handling middleware in Express?
  29. 29.What changed in Express 5 for async route handlers?
  30. 30.Does the order of app.use() calls matter in Express?
  31. 31.What is express.Router() used for?
  32. 32.What is a key feature of Fastify compared with Express?
  33. 33.Why validate request input on the server even if the front end already validates it?
  34. 34.Which HTTP methods are idempotent by definition?
  35. 35.What is a good way to return API errors in a consistent format?
  36. 36.How do you serve a folder of static files in Express?
  37. 37.How should passwords be stored?
  38. 38.What does a JWT signature guarantee?
  39. 39.What is a common downside of JWTs compared with server-side sessions?
  40. 40.Which cookie attributes help protect a session cookie?
  41. 41.Why must SQL queries use parameters instead of string concatenation?
  42. 42.What does the helmet middleware do?
  43. 43.Why add rate limiting to a login endpoint?
  44. 44.How do you generate a cryptographically secure random token?
  45. 45.How should you compare a secret token from a request with the stored one?
  46. 46.Why do Node.js apps use a database connection pool?
  47. 47.What is the N+1 query problem?
  48. 48.When do you need a database transaction?
  49. 49.What is an ORM such as Prisma or TypeORM?
  50. 50.Which Node.js version feature lets you use SQLite without installing a package?
  51. 51.What are worker threads for?
  52. 52.What is the difference between child_process.exec and child_process.spawn?
  53. 53.What does child_process.fork() do?
  54. 54.What does the cluster module do?
  55. 55.How do you mock a function with the built-in test runner?
  56. 56.How do you test an Express app's endpoints without starting a real server on a fixed port?
  57. 57.Which node:assert function compares objects by their contents?
  58. 58.What does the Twelve-Factor App recommend for configuration?
  59. 59.What should a Node.js server do when it receives SIGTERM?
  60. 60.Why should logs include a request ID?
  61. 61.Which data must never be written to logs?
  62. 62.In a Dockerfile for a Node.js app, why copy package.json and the lock file before the source code?
  63. 63.What is a health check endpoint like /health used for?
  64. 64.What does the exports field in package.json do?
  65. 65.Can CommonJS code require() an ES module in modern Node.js?
  66. 66.What does the engines field in package.json express?
  67. 67.What are npm workspaces?
  68. 68.What is a risk of postinstall scripts in dependencies?
  69. 69.When are Server-Sent Events (SSE) a better choice than WebSockets?
  70. 70.What is a challenge of WebSockets with several server instances?
  71. 71.Why use a job queue like BullMQ for sending emails after sign-up?
  72. 72.What is AsyncLocalStorage used for?
  73. 73.Why does HTTP keep-alive matter for a service that calls other APIs a lot?
  74. 74.Why is cursor-based pagination often better than OFFSET for large tables?
  75. 75.What is the cache-aside pattern with Redis?