Back to results for “HTTP & Web Servers”
Node.js › Express & Middleware

What is the difference between res.send(), res.json(), and res.end() in Express, and when should each be used?

Intermediate

Answer

res.json() explicitly serializes the given value to JSON and sets the Content-Type header to application/json, the most explicit and predictable choice for an API response. res.send() is more general-purpose, automatically detecting the type of the given argument (a string, buffer, or object, in which case it delegates to res.json() internally) and setting an appropriate Content-Type. res.end() sends the response immediately without any body processing, typically used to end a response that's already had its body written directly via res.write(), or to send a response with genuinely no body at all.
res.json({ status: 'ok' }); // explicit JSON, Content-Type: application/json
res.send('Hello world'); // Content-Type: text/html
res.send({ status: 'ok' }); // delegates to res.json() internally
res.status(204).end(); // no body at all, just ends the response
Real-world example An API standardizes on always using res.json() explicitly for every response (even simple ones) rather than the more ambiguous res.send(), making every response's Content-Type and serialization behavior explicit and predictable across the entire codebase.

Common follow-ups: Why might explicitly using res.json() everywhere be considered better practice than relying on res.send()'s automatic type detection?;What's the correct way to send a response with a 204 No Content status, given it shouldn't include a body at all?