Express res.send() Method
What you’ll learn
- How to send common response body types using
res.send(). - How to combine status codes and body output.
- How content type inference works with different payloads.
- How to avoid duplicate responses in route handlers.
Syntax
javascript
res.send([body])
res.status(200).send(body)1
Send plain text response
javascript
app.get('/ping', function (req, res) {
res.send('pong');
});2
Send object with status code
javascript
app.get('/status', function (req, res) {
res.status(200).send({ ok: true, server: 'up' });
});3
Return early on validation failure
javascript
app.get('/orders/:id', function (req, res) {
if (!/^\d+$/.test(req.params.id)) {
return res.status(400).send('Invalid order id');
}
res.send('Order #' + req.params.id);
});⚠️ Common pitfalls
- Do not call another response method after
res.send(). - Use early returns after error sends to avoid double responses.
- Prefer explicit APIs like
res.json()for JSON-only endpoints when clarity matters.
❓ FAQ
It sends a response body and finalizes the HTTP response.
Yes. It can send strings, buffers, objects, arrays, and other supported payloads.
res.json() is explicit for JSON APIs, while res.send() is a general-purpose sender with type inference.
Yes, chain status first, for example res.status(404).send('Not found').
Calling another response method after res.send(), which causes double-response errors.
Did you know?
res.send() automatically infers content type for common payloads and ends the response.
4 people found this page helpful
