Express.js res.send() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.send() sends the HTTP response body and ends the response. It sets Content-Length, picks a default Content-Type from the body type, and can add ETag / freshness support for simple (non-streaming) replies.

Use it for plain pages, small APIs, and error messages. Prefer res.json() when the endpoint is JSON-only. For files, use res.sendFile(). For templates, use res.render().

Syntax

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

Express
res.send([body])
res.status(code).send(body)
res.type(mime).send(body)
CallWhat it does
res.send([body])Primary signature.
res.status(code).send(body)Related call form.
res.type(mime).send(body)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.send([body])
res.status(code).send(body)
res.type(mime).send(body)

Parameters

ParameterDescription
bodyRequired. Payload to send. Defaults vary by type (see table below).
StringRequired. Override with res.type() / res.set()
Object / Array / BooleanRequired. JSON-serialized (same path as res.json)
BufferRequired. Binary; set type first if needed
undefinedRequired. Empty body

Return Value

res.send() sends the body, sets useful defaults when needed, and returns res. After it runs, the response is finished.

Express
return res.status(200).send('Hello');
return res.send({ ok: true }); // objects become JSON

Express.js Examples

Example 1 — Send a String

Default string type is HTML. Override when you need plain text.

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

app.get('/hello', (req, res) => {
  res.send('<h1>Hello</h1>');
});

app.get('/ping', (req, res) => {
  res.type('text/plain').send('pong');
});

app.listen(3000);

How It Works

  1. HTML path. A string alone defaults to text/html.
  2. Plain text path. res.type('text/plain') sets the header before send.
  3. Response ends. Content-Length is set; the client gets the body.
  4. No second send. The handler is done after send.

Example 2 — Send an Object with Status

Objects and arrays are sent as JSON. Chain status when needed.

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

app.get('/status', (req, res) => {
  res.status(200).send({ ok: true, server: 'up' });
});

app.get('/missing', (req, res) => {
  res.status(404).send({ error: 'Not found' });
});

app.listen(3000);

How It Works

  1. Status first. res.status(200) / 404 sets the code.
  2. Object → JSON. Express serializes and sets application/json.
  3. Same idea as res.json(). Prefer res.json() when the route is always JSON.
  4. Errors stay structured. Clients can parse error from the body.

Example 3 — Validate and Return Early

Send an error body and return so success code never runs.

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

app.get('/orders/:id', (req, res) => {
  if (!/^\d+$/.test(req.params.id)) {
    return res.status(400).send('Invalid order id');
  }
  res.send('Order #' + req.params.id);
});

app.listen(3000);

How It Works

  1. Validate first. Non-digits fail the regex.
  2. return after send. Stops the success res.send (no double response).
  3. 400 for bad input. Client knows the request was wrong.
  4. Valid ids continue. Only then is the order message sent.

Runtime Support

res.send() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only receive the finished HTTP response.

✓ Server · Express 4 & 5

res.send()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.send() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It sends an HTTP response body and ends the response. It sets Content-Length, can set Content-Type from the body type, and supports ETag / freshness checks for simple responses.
String, Buffer, Object, Array, Boolean, or undefined. Strings default to text/html; objects and arrays become JSON; Buffers default to application/octet-stream.
res.json() always serializes to JSON. res.send() is general-purpose: strings are treated as HTML unless you set the type first.
Yes. Chain status first: res.status(404).send('Not found'). Or use res.sendStatus() when you only need a status.
No. It runs on your Express server. The browser only receives the finished HTTP response.

Next: res.sendFile()

Transfer a file from disk with the correct Content-Type.

res.sendFile() 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