Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

res.end

res.end() finishes an Express HTTP response, optionally writing a string or Buffer first. Use it for empty or low-level responses; use res.send() or res.json() for Express-managed response data.

How it works

res.end() finishes the current HTTP response, optionally writing a string or Buffer first. It is inherited from Node.js’s http.ServerResponse; use it for an empty or low-level response, and prefer res.send() or res.json() when Express should format response data.

Syntax

res.end([data[, encoding]][, callback])

Parameters

NameTypeDescription
dataString | Buffer | undefinedString, Buffer, or undefined data to write before ending the response.
encodingString | undefinedEncoding used when data is a string.
callbackFunction | undefinedFunction called once the response has finished.

Returns

Response

Key points

  • Call res.end() when the response should finish without a body.
  • Pass a string or Buffer to write that data immediately before termination.
  • An encoding argument controls how string data is interpreted.
  • A callback can run after the response has completed.
  • The method closes the current response lifecycle.

Common mistakes

  • Do not use res.end() as the usual data-response method; choose res.send() or res.json() when Express should handle the payload.
  • Its behavior is provided by Node.js’s HTTP response implementation, so its lower-level semantics apply.

Example: End an empty response

const express = require('express');
const app = express();

app.get('/health', (req, res) => {
  res.status(204).end();
});

app.listen(3000);

Run this server, then request /health to receive an empty 204 response.

When to use it

Finish an empty response

Health checks and acknowledgements that require no response body.

Write and terminate raw data

Sending a small raw payload when Express formatting is unnecessary.

Return a status-only response

Setting a status code while returning no body.

Interview questions

QWhat does res.end() do in Express?

res.end() terminates the response process and can optionally write a string or Buffer before termination. It is useful when no response body is needed.

Follow-up: How would you return an empty not-found response? Chain res.status(404).end().

QWhich arguments can res.end() receive?

The method accepts optional data, an optional string encoding, and an optional completion callback. The data is written before the response is ended.

Follow-up: What data types are documented for the first argument? A string, Buffer, or undefined.

QWhat does res.end() return, and how can it be chained?

The method returns the Express Response object, allowing response methods to be chained. For example, res.status(404).end() sets the status before ending.

Follow-up: Why use res.send() or res.json() instead? They are the documented choices when the response should contain Express-managed data.

  • res.send()
  • res.json()
  • res.status()
  • http.ServerResponse.response.end()

Source: Response - Express.js 4.x. Checked 2026-10-15.