Mid
A mid-level answer names the mechanism and a failure mode.
When should a handler use a stream instead of a buffer?
Use a stream when the payload may be larger than you want in memory, such as a file proxy or a large response. Connect the readable to the writable and handle errors on the pipeline. Backpressure is the writable telling the readable to pause.
What keeps a Node process alive after the script's last line?
An open handle, such as a server, a timer, or a socket, keeps the loop running. A timer or a pool that was never closed is why a one-shot script hangs. In a server those handles are the job. In a script, close them.
When do you add processes, and when do you add worker threads?
Extra processes each have an event loop and a heap, which is how request work uses more cores. Worker threads share a process and can share memory, which fits CPU work off the request loop. Requests usually scale by process. A CPU task can use a worker.
How do you make a create endpoint safe to retry?
A retry must not create a second record. Accept an idempotency key, store the first success, and return that result when the key is repeated. A unique constraint in the database is the guard. A check-then-insert in memory is not, because two requests can pass the check together.
What is the difference between calling next with an error and rejecting a promise?
next(err) is an explicit hop into error middleware. A rejected promise from an async route is a different path, and on Express 4 it can become an unhandled rejection instead of that middleware. Wrap the route, or run a version that forwards rejections, and log the same way in both cases.
How do timeouts stop a slow client from holding a worker?
Set timeouts for headers, the body, and idle keep-alive sockets so a client that connects and stalls releases the process. The server timeout has to be longer than the upstream calls the handler makes, or the server will cancel work that would have succeeded.