0
0
ExpressComparisonBeginner · 4 min read

Res.send vs res.json vs res.end in Express: Key Differences and Usage

res.send sends a response with automatic content-type detection and can send strings, buffers, or objects. res.json specifically sends a JSON response by converting objects to JSON and setting the content-type to application/json. res.end simply ends the response without any data formatting or headers, used for low-level control.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of res.send, res.json, and res.end in Express.

Featureres.sendres.jsonres.end
PurposeSend response with auto content-typeSend JSON responseEnd response without data
Input TypesString, Buffer, Object, ArrayObject, Array (converted to JSON)Buffer or string (optional)
Content-Type HeaderSet based on input (e.g., text/html, application/json)Always application/jsonNot set automatically
Data FormattingConverts objects to JSON automaticallyConverts objects to JSON explicitlyNo formatting
Use CaseGeneral response sendingAPI JSON responsesLow-level response control or no content
Ends ResponseYesYesYes
⚖️

Key Differences

res.send is a versatile method that detects the type of data you pass and sets the appropriate Content-Type header automatically. For example, if you send a string, it sets text/html, and if you send an object or array, it converts it to JSON and sets application/json.

res.json is specialized for sending JSON responses. It always converts the input to a JSON string and sets the Content-Type header to application/json. This makes it ideal for APIs where JSON is expected.

res.end is a lower-level method that simply ends the HTTP response. It does not set headers or format data. You can optionally pass a string or buffer, but it will not modify or convert it. Use res.end when you want full control over the response or when sending no content.

⚖️

Code Comparison

javascript
import express from 'express';
const app = express();

app.get('/send', (req, res) => {
  res.send({ message: 'Hello from res.send' });
});

app.listen(3000);
Output
{"message":"Hello from res.send"}
↔️

res.json Equivalent

javascript
import express from 'express';
const app = express();

app.get('/json', (req, res) => {
  res.json({ message: 'Hello from res.json' });
});

app.listen(3000);
Output
{"message":"Hello from res.json"}
🎯

When to Use Which

Choose res.send when you want to send a response with flexible data types and automatic content-type handling, such as HTML, text, or JSON.

Choose res.json when you specifically want to send JSON data, especially in API responses, to ensure the correct content-type and formatting.

Choose res.end when you need to end the response manually without sending data or when you want full control over headers and response content.

Key Takeaways

res.send is flexible and auto-detects content type for various data.
res.json is specialized for sending JSON with correct headers.
res.end ends the response without formatting or headers.
Use res.json for APIs and res.send for general responses.
res.end is for low-level control or empty responses.