Skip to content
Tutorials Center
My learning

Topics

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.

NODE
import http from "node:http";

http.createServer((req, res) => {
  res.setHeader("Content-Type", "text/plain");
  res.end("done\n");
});

Try it

Node.js end() with data, without data, and the callback

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.

Node.js HEAD requests and ending twice

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

NODE
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 an ERR_STREAM_WRITE_AFTER_END error on res. Use res.writableEnded to guard.
  • end() without data on an already ended response does nothing.
  • A HEAD request, and status codes 204 and 304, get no body, even if you pass data.
  • If the connection has already closed, end() does not throw; the callback runs, but the data is lost.
  • Passing a number, object or null as data throws ERR_INVALID_ARG_TYPE. Convert with JSON.stringify() first.

Usage notes

  • A single res.end(body) with no earlier write() gives a Content-Length header. Using write() first switches to Transfer-Encoding: chunked, unless you set the length yourself.
  • If you promised a Content-Length and then send fewer bytes, the client waits for the rest or sees a truncated response.
  • end() does not mean the bytes reached the client: writableEnded becomes true immediately, while writableFinished and the finish event follow once the data is flushed.
  • In async handlers, make sure that every branch, including errors, calls end(). A try/catch that only logs leaves the request hanging.
  • After end(), do not touch res any more. Calling setHeader() throws ERR_HTTP_HEADERS_SENT.
  • pipeline(readable, res) calls end() for you when the source finishes.