Express res.end() Method
What you’ll learn
- How and when to end responses with
res.end(). - How to send optional final data using
res.end(data). - How
res.end()differs fromres.send(). - How to avoid duplicate response writes.
Syntax
javascript
res.end()
res.end([data[, encoding]])1
End response with status only
javascript
app.delete('/cache', function (req, res) {
// Cache cleared...
res.status(204).end();
});2
Send quick plain text and end
javascript
app.get('/ping', function (req, res) {
res.set('Content-Type', 'text/plain; charset=utf-8');
res.end('pong');
});3
Guard against double responses
javascript
app.get('/health', function (req, res) {
if (!process.env.DB_READY) {
return res.status(503).end('Service unavailable');
}
res.json({ ok: true });
});⚠️ Common pitfalls
- Do not call
res.send(),res.json(), or anotherres.end()after ending the response. - If you send data with
res.end(), set relevant headers manually when needed. - Use early returns to keep control flow clear after ending responses.
❓ FAQ
It ends the response process and tells Node/Express no more data will be written.
Yes, you can pass a string or buffer as the final response chunk.
res.send() handles content-type/format logic for you, while res.end() is lower-level and more manual.
Use it for lightweight plain responses or low-level streaming/control cases.
Calling another response method after res.end(), causing headers-sent or double-response errors.
Did you know?
res.end() finalizes the HTTP response stream and can optionally send a final data chunk.
4 people found this page helpful
