Node.js › Express & Middleware
What is the difference between res.send(), res.json(), and res.end() in Express, and when should each be used?
IntermediateAnswer
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 responseReal-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.