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.
| Feature | res.send | res.json | res.end |
|---|---|---|---|
| Purpose | Send response with auto content-type | Send JSON response | End response without data |
| Input Types | String, Buffer, Object, Array | Object, Array (converted to JSON) | Buffer or string (optional) |
| Content-Type Header | Set based on input (e.g., text/html, application/json) | Always application/json | Not set automatically |
| Data Formatting | Converts objects to JSON automatically | Converts objects to JSON explicitly | No formatting |
| Use Case | General response sending | API JSON responses | Low-level response control or no content |
| Ends Response | Yes | Yes | Yes |
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
import express from 'express'; const app = express(); app.get('/send', (req, res) => { res.send({ message: 'Hello from res.send' }); }); app.listen(3000);
res.json Equivalent
import express from 'express'; const app = express(); app.get('/json', (req, res) => { res.json({ message: 'Hello from res.json' }); }); app.listen(3000);
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.res.json for APIs and res.send for general responses.res.end is for low-level control or empty responses.