Bird
Raised Fist0
Expressframework~20 mins

Why understanding res matters in Express - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Express Response Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Express route send to the client?
Consider this Express route handler. What will the client receive as the response body?
Express
app.get('/hello', (req, res) => {
  res.status(200);
  res.send('Hello World');
});
AThe client receives 'Hello World' with HTTP status 200.
BThe client receives an empty response with HTTP status 200.
CThe client receives 'Hello World' with HTTP status 404.
DThe client receives an error because res.status must be called after res.send.
Attempts:
2 left
💡 Hint
Think about the order of res.status and res.send and what they do.
❓ state_output
intermediate
2:00remaining
What is the HTTP status code sent here?
Look at this Express handler. What status code will the client receive?
Express
app.get('/test', (req, res) => {
  res.send('Done');
  res.status(201);
});
A500 Internal Server Error
B201 Created
C200 OK
DNo response sent, request hangs
Attempts:
2 left
💡 Hint
Consider when the status code is set relative to sending the response.
🔧 Debug
advanced
2:00remaining
Why does this Express code cause an error?
This code throws an error when a request is made. What is the cause?
Express
app.get('/error', (req, res) => {
  res.send('First response');
  res.send('Second response');
});
Ares.send must be called with a status code first.
BCannot send multiple responses for one request; second res.send causes error.
Cres.send requires a callback function to avoid errors.
Dres.send can only send JSON, sending string causes error.
Attempts:
2 left
💡 Hint
Think about how many times you can send a response per request.
🧠 Conceptual
advanced
2:00remaining
What role does 'res' play in Express middleware?
In Express middleware functions, what is the main purpose of the 'res' object?
ATo build and send the HTTP response back to the client.
BTo parse incoming request data from the client.
CTo manage server-side database connections.
DTo handle routing and URL matching.
Attempts:
2 left
💡 Hint
Think about what happens after the server processes a request.
📝 Syntax
expert
2:00remaining
Which option correctly sends JSON with status 201?
You want to send a JSON response with HTTP status 201. Which code snippet does this correctly?
Ares.send({ success: true }).status(201);
Bres.json({ success: true }).status(201);
Cres.sendStatus(201).json({ success: true });
Dres.status(201).json({ success: true });
Attempts:
2 left
💡 Hint
Remember method chaining order matters in Express response.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To send responses back to the client -> Option C
  4. 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

  1. Step 1: Recall Express method for JSON response

    Express provides the res.json() method to send JSON data properly formatted to the client.
  2. Step 2: Check syntax correctness

    Only res.json() is the correct method name; others like sendJson or sendJSON do not exist.
  3. Final Answer:

    res.json({ message: 'Hello' }) -> Option A
  4. 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?
app.get('/hello', (req, res) => {
  res.status(200).send('Welcome!');
});
medium
A. Status 404 with message 'Welcome!'
B. No response sent
C. Status 500 with empty message
D. Status 200 with message 'Welcome!'

Solution

  1. Step 1: Analyze the status code set by res.status(200)

    The code sets the HTTP status to 200, which means OK or success.
  2. Step 2: Check the message sent with res.send('Welcome!')

    The string 'Welcome!' is sent as the response body to the client.
  3. Final Answer:

    Status 200 with message 'Welcome!' -> Option D
  4. Quick 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
A. res.json should be called before res.send
B. Calling res.send and res.json in the same handler causes an error
C. res.send cannot send strings
D. No error, code works fine

Solution

  1. Step 1: Understand response methods behavior

    Once res.send() sends a response, the connection closes and no further response can be sent.
  2. Step 2: Identify the problem with multiple sends

    Calling res.json() after res.send() tries to send a second response, causing an error.
  3. Final Answer:

    Calling res.send and res.json in the same handler causes an error -> Option B
  4. Quick 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
A. res.download('report.pdf');
B. res.sendFile('report.pdf');
C. res.send('report.pdf');
D. res.json({ file: 'report.pdf' });

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    res.download('report.pdf'); -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard