📖 Scenario: You are building a simple web server using Express. You want to send different types of responses to the client using res.send.
🎯 Goal: Learn how to use res.send to send text, HTML, and JSON responses in an Express server.
📋 What You'll Learn
Create an Express app
Set up a route that sends a plain text response using res.send
Set up a route that sends an HTML response using res.send
Set up a route that sends a JSON response using res.send
💡 Why This Matters
🌍 Real World
Web servers often need to send different types of responses like text, HTML pages, or JSON data to clients. Knowing how to use <code>res.send</code> helps you build flexible APIs and web apps.
💼 Career
Express is a popular web framework for Node.js used in many jobs. Understanding how to send responses correctly is a fundamental skill for backend developers.
Progress0 / 4 steps
1
Create an Express app and a text response route
Create an Express app by requiring express and calling express(). Then create a route for GET /text that sends the plain text response 'Hello, this is a text response!' using res.send.
Express
Hint
Use app.get to create the route and res.send to send the text.
2
Add a route that sends an HTML response
Add a new route for GET /html that sends a simple HTML string '
Welcome to the HTML page
' using res.send.
Express
Hint
Use app.get with the path /html and send the HTML string with res.send.
3
Add a route that sends a JSON response
Add a new route for GET /json that sends a JSON object { message: 'This is a JSON response' } using res.send.
Express
Hint
Use app.get with the path /json and send the JSON object with res.send.
4
Start the Express server
Add code to start the Express server listening on port 3000 using app.listen. Log the message 'Server is running on port 3000' when the server starts.
Express
Hint
Use app.listen with port 3000 and a callback that logs the message.
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?