comapre following method
res.send
res.json
res.end
Answer:
Following table explain difference betweeen 3.
Method What it does Content-Type Ends response?
res.send() Sends a response and automatically handles common data types Automatically sets appropriate Content-Type ✅ Yes
res.json() Sends a JSON response application/json ✅ Yes
res.end() Ends the HTTP response directly Does not automatically set Content-Type ✅ Yes
1. res.send()
res.send("hello");
typically results in:
Content-Type: text/html; charset=utf-8
2. res.json()
res.json({ "using": "json" });
Specifically sends a JSON response and sets:
Content-Type: application/json; charset=utf-8
3. res.end()
res.end("using end");
This directly ends the underlying HTTP response.
It does not provide Express's automatic formatting/content-type behavior like res.send() or res.json().
No comments:
Post a Comment