res.json()
res.json()
is a function that helps send a response in the form of JSON (JavaScript Object Notation) to the client. When you use this function, it automatically sets the type of content in the response to be JSON and sends the provided JavaScript object as the body of the response. This is particularly useful when your server needs to communicate with clients or other services in a structured and standardized data format.
res.send() |
res.json() |
---|---|
it is versatile, used for various response types. | It is specifically designed for sending JSON responses. |
It handles plain text, HTML, and other types. | it converts JavaScript objects/arrays to JSON format. |
It automatically set based on provided data. | It automatically set to application/json . |
Example : javascript res.send ('Text response') ; |
Example: javascript res.json ({ key: 'value' }) ; |