res.send() and res.json() Functions in Express.js
Overview
Do you know about the objects in express? How does a user request, and receive the data? What is res.send and res.json in express?
Request and Response are the two objects in the express. The user receives the data through the req object via three instances req.param, req.body and req.query. Responses are nothing but the replies given by the express app when a user makes an HTTP request for the data. When a user performs res.send in the app, it cannot be repeated for the same data.
Introduction to Sending Response in Express
Express is a popular web application framework that can handle incoming HTTP requests and send responses back to the client. It can be used to send a variety of data types such as plain text, HTML, binary data, or JSON. We can send plain text responses as well as JSON responses.
There are many other methods for sending responses in the express. It depends on the type of data we need to send. res.send() can be used to send the responses with different parameters. There are various parameters used for responses such as HTML, some custom status codes, file path, and header. By analyzing the types of data we can build powerful web applications that can handle various use cases.
Sending Response in Express
In express, there are two types of responses: res.send and res.json. Both of the responses are used for different types of data. So let’s discuss res.send and res.json with examples.
res.send
Syntax
res.send is used to send the HTTP response. As we have already mentioned, there can be different types of parameterized responses. Body parameters can be a string, object, or array. It returns an object to the client.
Example
Output
After running the above code, the following output will be generated:
In the above example, we have required express to use in our web application. urlencoded function is used to parse the incoming requests with urlencoded payloads and it is based on the body parser. we have defined a route for the root path ('/'). When a GET request is made to this route, the route handler function is called with two arguments: req( the request object) and res( the response object).
res.json
Let’s suppose we want to send multiple files, then we will use the res.json() function.
Syntax
Example
Output
After running the above code, the following output will be generated:
In the above example, we are defining a route for '/users'. When a GET request is made to this route, an array is created which consists of user objects, and the res.json() method will be used to send it back to the client as json.
Conclusion
- There are two types of responses: res.send and res.json. The most preferable method is end, which lets us send both binary data and textual data.
- Different types of data will give different types of responses.
- res.json is used to send multiple data at the same time like an array.
- various methods allow the users to customize the responses based on their specific needs.
- There are various parameters used for sending responses such as text, binary, file path, and HTML.