The Wayback Machine - https://web.archive.org/web/20240612131946/https://www.geeksforgeeks.org/difference-between-res-send-and-res-json-in-express/
Open In App

Difference between res.send() and res.json() in Express

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

res.send():

res.send() is a method used to send a response back to the client. It’s a versatile function that can handle various types of responses, such as HTML, plain text, or even binary data. When you use res.send(), Express automatically sets the appropriate Content-Type header based on the type of data you’re sending.

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' });

Similar Reads

Difference between res.send() and res.json() in Express.js?
In this article, we will learn about res.send() & res.json(), along with discussing the significant distinction that differentiates between res.send() & res.json(). Let us first understand what is res.send() and res.json() in Express.js? res.send() - The res.send() function is used for sending the response for the HTTP request. It takes a p
3 min read
How to Exit after res.send() in Express JS
In this article, we are going to learn how we can exit after the res.send() function in Express JS. In Express the res.send() method is mainly used to send a response to the client with the specified content. This function automatically sets the Content-Type Header which is based on the data provided. We are going to implement the Exit after res.se
3 min read
Express res.send() Function
The res.send() function sends the HTTP response. The body parameter can be a String or a Buffer object or an object or an Array. Syntax:  res.send( [body] )Parameter: This function accepts a single parameter body that describes the body to be sent in the response. Returns: It returns an Object. Steps to Install the Express Module:Step 1: You can in
2 min read
Express Returns Servers HTML Error Page Instead of res.json Error Message
ExpressJS is a backend web application framework built on top of NodeJS which helps us manage the server and routing. It has some powerful functionalities like middleware, templates, and debugging which help us to write efficient and faster code. It helps us extensively in building web applications. In this article, we will see why "Express returns
3 min read
Express res.json() Function
The res.json() function sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using the JSON.stringify() method. Syntax:   res.json( [body] )Parameters: The body parameter is the body that is to be sent in the response. Return Value: It returns an Object. Steps to Instal
2 min read
How to intercept response.send() / response.json() in Express JS
In the context of Express , "intercept" usually refers to the process of capturing or modifying a request or response in a middleware function before it reaches its final destination (e.g., a route handler) or before it is sent back to the client. In Express, intercepting response.send() or response.json() can be achieved by using middleware functi
3 min read
Express JS express.json() Function
The express.json() function is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser.  Syntax: express.json( [options] )Parameters: The options parameter has various properties like inflate, limit, type, etc.  Return Value: It returns an Object.  Steps to Installation the express modul
2 min read
How to send Email using Git send-email via Gmail?
Git's send-email feature provides a convenient way to send patches and other Git-related content via email directly from the command line. This can be particularly useful for submitting patches to mailing lists or collaborating with team members who prefer email communication. In this article, we'll focus on setting up and using git send-email via
3 min read
Explain the use of req and res objects in Express JS
Express JS is used to build RESTful APIs with Node.js. We have a 'req' (request) object in Express JS which is used to represent the incoming HTTP request that consists of data like parameters, query strings, and also the request body. Along with this, we have 'res' (response) which is used to send the HTTP response to the client which allows the m
3 min read
Express.js res.location() Function
The res.location() function is used to set the response Location HTTP header to the path parameter which is being specified. Basically, it is used to set the response header. It doesn't end the response, after using it you can write a response body if you want to write. Syntax: res.location( path ) Parameter: path: It refers to the URL which is spe
2 min read