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. What are streams in Node.js and why would you use them for large files?

    A stream processes data in small chunks as it arrives instead of loading everything into memory at once. For a large file, streams keep memory use low and let you start sending data immediately, and pipeline() handles backpressure and errors for you.

    IntermediateIn-depth3 min read
  2. What are the four types of streams in Node.js?

    Readable (data source, like a file read), Writable (destination, like a file write or HTTP response), Duplex (both, like a TCP socket), and Transform (a duplex that changes data as it passes, like gzip).

    BeginnerQuick answer1 min read
  3. How do you read a very large file line by line in Node.js?

    Create a read stream and pass it to readline.createInterface, then loop with for await. Only a small chunk sits in memory at a time, unlike readFile, which loads the whole file.

    IntermediateQuick answer1 min read
  4. 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
  5. What is backpressure in Node.js streams?

    Backpressure is when a writable cannot consume data as fast as a readable produces it. The writable signals this, and the reader should pause, otherwise memory fills up with unwritten data.

    IntermediateQuick answer1 min read
  6. What is the difference between stream.pipe and stream.pipeline?

    pipe connects streams but does not forward errors or clean up, so a failure can leave streams open. pipeline connects several streams, destroys all of them on error, and reports the result in one callback or promise.

    IntermediateQuick answer1 min read
  7. Write a Transform stream that converts incoming text to uppercase.

    Extend stream.Transform (or pass a transform function), call this.push with the changed chunk, and call the callback when done. Then pipe a readable into it and into a writable.

    IntermediateQuick answerCoding1 min read