expressjs
  1. expressjs-sending-responses

ExpressJs Sending Responses

Syntax

res.send([body])

Example

app.get('/', function(req, res) {
  res.send('Hello World!');
});

Output

The above example sends the "Hello World!" string as the response body.

Explanation

In Express.js, you can send responses using the send() method of the response object (res). The send() method is used to send the HTTP response to the client with a specified response body. The response body can be of any type, but it is typically a string or an object.

Use

res.send() method can be used to send various types of responses like JSON, HTML, text, and XML.

Important Points

  • res.send() method sets the Content-Type header of the response based on the type of data provided in the response.
  • res.status() method can be used to set the HTTP status of response if needed.

Summary

In this tutorial, we have learned about the res.send() method in Express.js that is used to send responses to the client with a specified response body. It is an important method that is used in almost all Express.js applications. We have also discussed its syntax, example, output, explanation, use, and some important points.

Published on: