res.send is used to send a response back to the client in a simple way. It helps your server talk to the browser or app by sending text, HTML, or data.
When you want to send a simple message or text back to the user.
When you want to send HTML content as a response.
When you want to send JSON or other data formats quickly without extra setup.
When you want to end the response and tell the client you are done sending data.
Syntax
Express
res.send(body)
body can be a string, a buffer, an object, or an array.
If you send an object or array, Express automatically converts it to JSON.
Examples
Sends a simple text message to the client.
Express
res.send('Hello World!')
Sends a JSON object. Express converts it to JSON format automatically.
Express
res.send({ message: 'Success' })
Sends HTML content that the browser will render as a heading.
Express
res.send('<h1>Welcome</h1>')
Sample Program
This simple Express server has two routes. The root route sends a plain text message. The /json route sends a JSON object. Both use res.send to send responses.
Using res.send automatically sets the correct Content-Type header based on the data you send.
Calling res.send ends the response, so you should not call it more than once per request.
Summary
res.send sends a response back to the client easily.
It works with text, HTML, JSON, and more.
It automatically sets headers and ends the response.
Practice
(1/5)
1. What does res.send() do in an Express app?
easy
A. Logs information to the console
B. Sends a response back to the client and ends the request
C. Reads data from the client request
D. Starts a new server
Solution
Step 1: Understand the purpose of res.send()
res.send() is used to send a response back to the client in Express.
Step 2: Recognize that it ends the response
After sending data, res.send() automatically ends the response, so no more data is sent.
Final Answer:
Sends a response back to the client and ends the request -> Option B
Quick Check:
res.send() = sends response [OK]
Hint: Remember: res.send sends and finishes response [OK]
Common Mistakes:
Thinking res.send starts the server
Confusing res.send with reading request data
Assuming res.send logs to console
2. Which of the following is the correct syntax to send a plain text response using res.send?
easy
A. res.send('Hello');
B. res.send[Hello];
C. res.send({ message: 'Hello' });
D. res.send = 'Hello';
Solution
Step 1: Identify correct method call syntax
The correct way to call res.send is as a function with parentheses and the data inside.
Step 2: Check the options for valid syntax
res.send('Hello'); uses res.send('Hello'); which is correct. res.send({ message: 'Hello' }); sends an object, which is valid but not plain text. Options B and D have invalid syntax.
Final Answer:
res.send('Hello'); -> Option A
Quick Check:
Correct function call = res.send('text') [OK]
Hint: Use parentheses and quotes: res.send('text') [OK]
Common Mistakes:
Using square brackets instead of parentheses
Assigning value instead of calling function
Sending object when plain text is needed
3. What will the client receive when this Express route runs?