The res object in Express helps you send responses back to the user. Understanding it lets you control what the user sees after they make a request.
Why understanding res matters in Express
Introduction
Syntax
Express
res.status(code).send(body) res.json(object) res.redirect(url) res.sendFile(path)
res.status(code) sets the HTTP status code for the response.
res.send() sends a string or buffer as the response body.
Examples
Express
res.status(200).send('Hello World!')
Express
res.json({ message: 'Success' })Express
res.redirect('/home')Express
res.sendFile('/path/to/file.jpg')Sample Program
This Express app shows how res sends different responses: a text message, JSON data, and a redirect.
Express
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.status(200).send('Welcome to the homepage!'); }); app.get('/json', (req, res) => { res.json({ greeting: 'Hello, JSON!' }); }); app.get('/redirect', (req, res) => { res.redirect('/'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Important Notes
Always set the correct status code to help browsers and tools understand the response.
Use res.json() when sending data to APIs for easier parsing.
Remember to end your response with res.send(), res.json(), or similar methods to avoid hanging requests.
Summary
The res object controls what the server sends back to the user.
It can send text, JSON, files, or redirects.
Understanding res helps you build clear and useful web responses.
Practice
1. What is the main purpose of the
res object in Express?easy
Solution
Step 1: Understand the role of
Theresin Expressresobject represents the response that the server sends back to the client after processing a request.Step 2: Differentiate
Theresfrom other objectsreqobject handles incoming requests, whilerescontrols what is sent back, such as text, JSON, or files.Final Answer:
To send responses back to the client -> Option CQuick Check:
ressends 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
Solution
Step 1: Recall Express method for JSON response
Express provides theres.json()method to send JSON data properly formatted to the client.Step 2: Check syntax correctness
Onlyres.json()is the correct method name; others likesendJsonorsendJSONdo not exist.Final Answer:
res.json({ message: 'Hello' }) -> Option AQuick Check:
Useres.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?
app.get('/hello', (req, res) => {
res.status(200).send('Welcome!');
});medium
Solution
Step 1: Analyze the status code set by
The code sets the HTTP status to 200, which means OK or success.res.status(200)Step 2: Check the message sent with
The string 'Welcome!' is sent as the response body to the client.res.send('Welcome!')Final Answer:
Status 200 with message 'Welcome!' -> Option DQuick Check:
Status 200 + send message = Status 200 with message 'Welcome!' [OK]
Hint: res.status sets code; res.send sends message [OK]
Common Mistakes:
- Assuming default status is 404
- Thinking res.send changes status code
- Forgetting to send a response
4. Identify the error in this Express code snippet:
app.get('/data', (req, res) => {
res.send('Data loaded');
res.json({ success: true });
});medium
Solution
Step 1: Understand response methods behavior
Onceres.send()sends a response, the connection closes and no further response can be sent.Step 2: Identify the problem with multiple sends
Callingres.json()afterres.send()tries to send a second response, causing an error.Final Answer:
Calling res.send and res.json in the same handler causes an error -> Option BQuick Check:
Only one response per request [OK]
Hint: Send only one response per request [OK]
Common Mistakes:
- Trying to send multiple responses
- Assuming order of send/json fixes issue
- Thinking res.send can't send strings
5. You want to send a file named
report.pdf to the client using res. Which code snippet correctly achieves this in Express?hard
Solution
Step 1: Understand methods to send files in Express
res.sendFile()sends a file as response, but requires an absolute path or root option;res.download()prompts the client to download the file.Step 2: Choose method that correctly sends file for download
res.download('report.pdf')sends the file with headers to trigger download, which is usually desired for files like PDFs.Final Answer:
res.download('report.pdf'); -> Option AQuick Check:
Use res.download to send files for download [OK]
Hint: Use res.download for file downloads [OK]
Common Mistakes:
- Using res.send for files (sends text)
- Using res.json to send file name only
- Using res.sendFile without absolute path
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
