📖 Scenario: You are building a simple Express server that sends responses to users. Understanding how the res object works is important to send the right messages back.
🎯 Goal: Build a basic Express server that uses the res object to send a text message and a JSON response to the client.
📋 What You'll Learn
Create an Express app variable called app
Create a route handler for GET requests on /hello
Use res.send() to send a plain text message
Create a route handler for GET requests on /data
Use res.json() to send a JSON object
💡 Why This Matters
🌍 Real World
Express servers are used to build web applications and APIs that send data and pages to users. Knowing how to use <code>res</code> helps you control what users see.
💼 Career
Backend developers must understand how to send correct responses using <code>res</code> in Express to build reliable and user-friendly web services.
Progress0 / 4 steps
1
Set up Express app
Create a variable called express that requires the 'express' module. Then create a variable called app by calling express().
Express
Hint
Use require('express') to import Express and then call it to create the app.
2
Add a simple text response route
Add a GET route handler on /hello using app.get. The handler function should take req and res as parameters. Use res.send() to send the text message 'Hello, world!'.
Express
Hint
Use app.get('/hello', (req, res) => { res.send('Hello, world!'); }) to send a text response.
3
Add a JSON response route
Add a GET route handler on /data using app.get. The handler function should take req and res as parameters. Use res.json() to send the JSON object { message: 'This is JSON data' }.
Express
Hint
Use res.json({ message: 'This is JSON data' }) to send JSON data.
4
Start the Express server
Add code to start the server by calling app.listen on port 3000. Use a callback function that logs 'Server running on port 3000'.
Express
Hint
Use app.listen(3000, () => { console.log('Server running on port 3000'); }) to start the server.
Practice
(1/5)
1. What is the main purpose of the res object in Express?
easy
A. To manage database connections
B. To handle incoming requests from the client
C. To send responses back to the client
D. To configure server settings
Solution
Step 1: Understand the role of res in Express
The res object represents the response that the server sends back to the client after processing a request.
Step 2: Differentiate res from other objects
The req object handles incoming requests, while res controls what is sent back, such as text, JSON, or files.
Final Answer:
To send responses back to the client -> Option C
Quick Check:
res sends responses [OK]
Hint: Remember: res = response sent back to user [OK]
Common Mistakes:
Confusing res with req
Thinking res manages server setup
Assuming res handles database
2. Which of the following is the correct way to send a JSON response using res in Express?
easy
A. res.json({ message: 'Hello' })
B. res.sendJSON({ message: 'Hello' })
C. res.sendJson({ message: 'Hello' })
D. res.send({ json: 'Hello' })
Solution
Step 1: Recall Express method for JSON response
Express provides the res.json() method to send JSON data properly formatted to the client.
Step 2: Check syntax correctness
Only res.json() is the correct method name; others like sendJson or sendJSON do not exist.
Final Answer:
res.json({ message: 'Hello' }) -> Option A
Quick Check:
Use res.json() for JSON [OK]
Hint: Use exact method name: json, not sendJson [OK]
Common Mistakes:
Using incorrect method names like sendJson
Confusing res.send with res.json
Passing wrong argument structure
3. What will be the output sent to the client when this Express handler runs?