Interview Q&A / Node.js

INTERVIEW Q&A · 68 QUESTIONS

Node.js interview questions

The event loop on the server, streams, modules and building APIs with Express.

  1. How do you send a large file to the browser without loading it all in memory?

    Create a read stream of the file and pipe it into the HTTP response. Set the Content-Type and, if known, Content-Length headers first, and handle stream errors before any bytes are sent.

    IntermediateQuick answer1 min read
  2. How does Express know a middleware is an error handler?

    By its arity: an error-handling middleware takes four arguments (err, req, res, next). It must be registered after the routes, and errors reach it through next(err) or a thrown error in synchronous code.

    IntermediateQuick answer1 min read
  3. What is CORS and how do you enable it safely in an Express API?

    CORS is a browser rule that blocks a page from reading responses from a different origin unless the server allows it with headers. In Express, use the cors package with an allow-list of origins, not a wildcard for private APIs.

    IntermediateQuick answer1 min read