Express.js res.end() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

res.end() ends the response process. It comes from Node core — http.ServerResponse.prototype.end — and Express exposes it on res. After you call it, do not write more body or call res.send() / res.json().

Use it to finish quickly with no body (or after you already set status/headers). Official guidance: if you need to respond with data, prefer res.send() or res.json() instead.

Syntax

Call res.end in your Express.js app. Start with this line.

Express
res.end([data[, encoding]][, callback])
res.end();
res.status(404).end();
CallWhat it does
res.end([data[, encoding]][, callback])Primary signature.
res.end();Related call form.
res.status(404).end();Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.end([data[, encoding]][, callback])
res.end();
res.status(404).end();

Parameters

ParameterDescription
dataRequired. Final chunk to write before ending. Prefer send/json for typical bodies.
encodingRequired. Encoding when data is a string (Node default applies if omitted).
callbackRequired. Called once the response has finished.

Return Value

res.end() finishes the response and returns res. Prefer res.send() / res.json() when you have a body; use end for empty or low-level finishes.

Express
res.status(204).end();
// response complete — do not send again

Express.js Examples

Example 1 — End with Status Only (204)

Finish a DELETE (or similar) with no body — classic use of end.

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

app.delete('/cache', (req, res) => {
  // Cache cleared...
  res.status(204).end();
});

app.listen(3000);

How It Works

  1. Set the status. res.status(204) marks success with no content.
  2. End with no data. end() closes the response without a body.
  3. Matches official examples. Docs show res.end() and res.status(404).end() for quick finishes.
  4. Client gets headers only. Ideal for DELETE/PUT side effects with nothing to return.

Example 2 — Send a Small Chunk with end

Low-level plain text. For most apps, res.send('pong') is clearer.

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

app.get('/ping', (req, res) => {
  res.set('Content-Type', 'text/plain; charset=utf-8');
  res.end('pong');
});

// Preferred for most cases:
// res.type('text').send('pong');

app.listen(3000);

How It Works

  1. Set Content-Type yourself. end does not infer types like send does.
  2. Pass the final chunk. res.end('pong') writes the string and closes.
  3. No more writes. Calling send afterward would error (headers already sent).
  4. Docs preference: for data responses, reach for send/json first.

Example 3 — Guard Against Double Responses

End early on failure, then return so the happy path does not send again.

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

app.get('/health', (req, res) => {
  if (!process.env.DB_READY) {
    return res.status(503).end('Service unavailable');
  }
  res.json({ ok: true });
});

app.listen(3000);

How It Works

  1. Branch on a condition. Unready DB → finish with 503 and end.
  2. return after end. Stops the handler from reaching res.json.
  3. Happy path uses json. Only one finish method runs per request.
  4. Avoids ERR_HTTP_HEADERS_SENT. The classic bug is ending, then sending again.

Runtime Support

res.end() is the Node.js http.ServerResponse method, available on Express res. It runs on the server (Express 5 needs Node.js 18+). Browsers only receive the finished HTTP response.

✓ Server · Node core + Express

res.end()

Works wherever Express runs on a Node-compatible HTTP stack. Bun and Deno can run many Express apps with compatibility layers. Not a browser API.

Server Node.js runtime API
Node.js 18+ for Express 5 · core http.ServerResponse
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.end() Node http.ServerResponse

Bottom line: Study and run these examples with Node.js and the express package. Prefer send/json for everyday response bodies.

Frequently Asked Questions

It ends the response process. It comes from Node’s http.ServerResponse. After end, you must not write more body or call send/json again.
Use end for empty or status-only replies (for example 204 No Content, or res.status(404).end()). For normal bodies, prefer res.send() or res.json().
Yes — you can pass a string or Buffer. Express docs still recommend send/json for typical data responses because they set Content-Type and related helpers for you.
It sets the status code, then ends with an empty body. Useful for simple error or no-content responses.
No. It is a stable Node/Express response method used widely for finishing responses.

Next: res.format()

Negotiate the response body from the request Accept header.

res.format() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful