Q11: What is the difference between `res.send()`, `res.json()`, and `res.end()`?
res.send(): Generic response method. Accepts Buffer, String, Object, or Array. Automatically setsContent-Typeheader based on parameter type.res.json(): Explicitly formats input parameter usingJSON.stringify()and explicitly setsContent-Type: application/jsonheader. Convertsnullandundefinedto valid JSON.res.end(): Node native method used to end response cycle quickly without sending data or auto-setting headers. Useful for 204 No Content responses.
Key Takeaways
Use
res.json()when building RESTful APIs to guarantee proper JSON response headers.res.send()invokesres.json()internally when passed an Object or Array.