Difference between res.send() and res.json() in Express.js?
Last Updated :
24 Apr, 2025
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 parameter body. The parameter can be a String, Buffer object, an object, Boolean, or an Array.
Syntax:
res.send( [body] )
Parameter: The body parameter takes a single argument that describes the body that is to be sent in the response.
Content Type: The Express sets the content type of the parameter according to the type of the body parameter.
- String & HTML - “text/html”
- Buffer - “application/octet-stream”
- Array & object - "application/json"
Return Type: It is used to send a response to the client and then ends the response process. The method itself doesn't return anything.
Example: This example demonstrate the simple res.send().
JavaScript
const express = require('express');
const app = express();
const PORT = 8000;
app.get('/', (req, res)=>{
res.send('Hello World')
});
app.listen(PORT, ()=>{
console.log(`Server is listening to port: ${PORT}`)
});
Output:
Server is listening to port: 8000
Simple example of res.send()
res.json() - The res.json() function sends the response as JSON format. It automatically converts the JavaScript object into a JSON-formatted string. The parameter body can be of any JSON type, which includes object, array, string, Boolean, number, or null.
res.json() will also convert non-objects, such as null and undefined, which are not valid JSON.
Syntax:
res.json( [body] )
Parameter: The body parameter takes JavaScript Object as its argument.
Content Type: Its content-type is set to - "application/json".
Returns: It is used to send JSON response to the client and then ends the response process.
Example: This example demonstrate the simple res.send().
JavaScript
const express = require('express');
const app = express();
const PORT = 8000;
const data = [
{id: 1, name: 'GFG'},
{id: 2, name: 'Express'}
]
app.get('/', (req, res)=>{
res.json(data)
});
app.listen(PORT, ()=>{
console.log(`Server is listening to port: ${PORT}`)
});
Output:
Server is listening to port: 8000
Simple example of res.json()Difference between res.send() and res.json():
Through both res.send() and res.json() seems to identical and fulfill similar purposes but still there is significant contrast that differentiates both res.send() and res.json(), which is given below.
|
The Content-Type header is automatically set based on the type of body parameter being sent.
| The Content-Type header is explicitly set to 'application/json', indicating that the response contains JSON data.
|
This method is used as general-purpose and can be used to handle various types of data and the content type will also set automatically according to the data.
| This method is specially designed to send JSON response means it will convert all type of body params into JSON object.
|
This method doesn't perform any automatic JSON stringification.
| This method automatically converts the JavaScript Object passed into it to JSON-formatted string.
|
It doesn't convert or format the data given to them.
| You can format the returned JSON data by using json replacer, json spaces etc.
|
More versatile and can be used when you want to send different types of data without explicitly indicating that it's JSON.
| Specifically used when you want to send JSON-formatted data and want to be explicit about it.
|
Conclusion:
Both res.send() and res.json() serves similar purposes with some difference. So it depends on the data type which we are working with. Choose res.json() when you are specifically working with JSON data. Use res.send() when you need versatility and control over the content type or when dealing with various data types in your responses.
Similar Reads
Difference Between req.query and req.params in Express
In Express, req.query and req.params are used to access different types of parameters in a request. 'req.query' retrieves query string parameters from the URL (e.g., '/search?name=GFG' â 'req.query.name' is '"GFG"'), while 'req.params' retrieves route parameters defined in the URL path (e.g., '/user
3 min read
Difference between req.cookies and req.signedCookies in Express.js
req.cookies: Request. Cookies are supposed to be cookies that come from the client (browser) and Response. Cookies are cookies that will send back to the client (browser). Cookies are small files/data that are sent to the client with a server request and stored on the client side. This helps us to k
3 min read
Difference between module.exports and exports in Node.js
module.exports is used when there is only a single item e.g., function, object or variable that needs to be exported, while exports is used for multiple items. The module is a plain JavaScript Object representing the current module. It is local to each module and also it is private. It has exports p
3 min read
Difference between sessions and cookies in Express
Express.js is a popular framework for Node.js, that is used to create web applications. It provides tools to manage user sessions and cookies. The session and cookies are used to maintain the state and manage user authentication. In this article, we will learn about what sessions and cookies in Expr
4 min read
Difference between app.use() and app.get() in Express.js
Express.js, a popular web application framework for Node.js, provides a powerful set of methods for routing HTTP requests. Among these methods, app.use() and app.get() are two fundamental functions that serve different purposes. Understanding their differences is essential for building efficient and
4 min read
Difference between app.get() and app.post() in Express.js.
In ExpressJS, app.get() and app.post() are two different methods used to handle HTTP requests but for different purposes. app.get() in ExpressJS:app.get() is used to handle incoming GET requests. GET requests are commonly used for fetching data from the server. For example, when you open a website i
2 min read
Difference between React.js and Node.js
1. React.js : This is the Javascript library that was developed by Facebook in 2013. This library was developed in order to improve and enhance the UI for the web apps but with time it has evolved a lot. It is open-source and supports different inbuilt functions and modules like form module, routing
2 min read
Difference between PUT and POST HTTP Request in Express and Postman
Both PUT and POST are request methods used in the HTTP protocol. In this article, we will introduce the HTTP methods such as PUT and POST in detail, and we will also discuss in detail when to use PUT and when to use POST. Table of Content PUT methodPOST MethodDifference between PUT and POST methodCo
6 min read
Difference Between Express and Fastify Web App Frameworks
While building backend applications with Node JS there is a list of frameworks that can be used with Node JS. Two of the most popular choices are Express.js and Fastify. In this article, we will learn about these frameworks and their difference. Table of Content What is Express JS?Features of Expres
3 min read
What is the difference between â(â¦);â and â{â¦}â in ReactJS ?
When you write JavaScript, you can use either the "(â¦)" or "{â¦}" pattern to define objects. In ReactJS, (...); and {...} are used in different contexts and have different purposes, and are used to denote different types of code structures. What is "(â¦);" in React JS ?In ReactJS, (...); is used to de
5 min read