Bird
0
0

Which of the following is the correct syntax to send a 201 status with a JSON response in Express?

easy📝 Syntax Q3 of 15
Node.js - HTTP Module
Which of the following is the correct syntax to send a 201 status with a JSON response in Express?
Ares.json(201, { message: 'Created' })
Bres.sendStatus(201, { message: 'Created' })
Cres.status(201).send('Created')
Dres.status(201).json({ message: 'Created' })
Step-by-Step Solution
Solution:
  1. Step 1: Understand chaining methods

    res.status(201) sets status, then .json() sends JSON data.
  2. Step 2: Check other options for syntax errors

    res.sendStatus() does not accept data; res.json() does not take status as first argument; res.status(201).send('Created') sends plain text, not JSON.
  3. Final Answer:

    res.status(201).json({ message: 'Created' }) -> Option D
  4. Quick Check:

    Set status then send JSON with .json() [OK]
Quick Trick: Chain res.status(code).json(data) to send JSON with status [OK]
Common Mistakes:
  • Passing status code as first argument to res.json()
  • Using res.sendStatus() with data argument
  • Using res.send() with string for JSON response

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes