Node.js Reference ยท HTTP, HTTPS, net, TLS, dgram and dns
Node.js res.end() method
Reference for response.end(): finish an HTTP response, send a final chunk, content-length behavior, HEAD requests, the callback and what happens when you forget it.
res.end([data[, encoding]][, callback]) finishes an HTTP response. It tells Node that you have sent everything, so the message can be completed and the connection reused or closed. If you pass data, it is written as the last chunk first, which makes res.end("text") the usual way to send a small reply.
Every request needs exactly one end(). If a code path forgets it, the client keeps waiting until a timeout. end() also sends the headers if nothing was written yet, and when the whole body is given in one call and you set no Content-Length, Node computes it for you.
import http from "node:http"; http.createServer((req, res) => { res.setHeader("Content-Type", "text/plain"); res.end("done\n"); });
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
res.end(callback?: () => void): this res.end(data: string | Buffer | Uint8Array, callback?: () => void): this res.end(data: string, encoding: BufferEncoding, callback?: () => void): this
| Name | Description |
|---|---|
data |
A final chunk to write before finishing. A string or a Buffer/Uint8Array. |
encoding |
Encoding of a string data. Default "utf8". |
callback |
Called when the response has been fully flushed (the finish event). |
Returns: the response (this).
Errors and edge cases:
end(data)after the response already ended emits anERR_STREAM_WRITE_AFTER_ENDerror onres. Useres.writableEndedto guard.end()without data on an already ended response does nothing.- A
HEADrequest, and status codes204and304, get no body, even if you passdata. - If the connection has already closed,
end()does not throw; the callback runs, but the data is lost. - Passing a number, object or
nullasdatathrowsERR_INVALID_ARG_TYPE. Convert withJSON.stringify()first.
Usage notes
- A single
res.end(body)with no earlierwrite()gives aContent-Lengthheader. Usingwrite()first switches toTransfer-Encoding: chunked, unless you set the length yourself. - If you promised a
Content-Lengthand then send fewer bytes, the client waits for the rest or sees a truncated response. end()does not mean the bytes reached the client:writableEndedbecomestrueimmediately, whilewritableFinishedand thefinishevent follow once the data is flushed.- In async handlers, make sure that every branch, including errors, calls
end(). Atry/catchthat only logs leaves the request hanging. - After
end(), do not touchresany more. CallingsetHeader()throwsERR_HTTP_HEADERS_SENT. pipeline(readable, res)callsend()for you when the source finishes.