0
0
Node.jsframework~10 mins

Error response formatting in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a JSON error response with status 400.

Node.js
res.status(400).json({ error: [1] });
Drag options to blanks, or click blank then click option'
A"Invalid input"
B400
Ctrue
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number instead of a string for the error message.
Sending null or true instead of a descriptive message.
2fill in blank
medium

Complete the code to set the HTTP status to 404 before sending the error JSON.

Node.js
res.[1](404).json({ error: "Not found" });
Drag options to blanks, or click blank then click option'
Asend
Bjson
Cstatus
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of status to set the status code.
Trying to set status with set which is for headers.
3fill in blank
hard

Fix the error in the code to properly send a JSON error response with status 500.

Node.js
res.status(500).[1]({ message: "Server error" });
Drag options to blanks, or click blank then click option'
Asend
Bjson
Cwrite
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using send which may not set the content type correctly.
Using write or end which are lower-level methods.
4fill in blank
hard

Fill both blanks to create a middleware that sends a 401 error with a JSON message.

Node.js
app.use((req, res) => { res.[1]([2]).json({ error: "Unauthorized" }); });
Drag options to blanks, or click blank then click option'
Astatus
Bsend
C401
D403
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of status to set the status code.
Using 403 instead of 401 for unauthorized errors.
5fill in blank
hard

Fill all three blanks to send a 422 error with a JSON object containing code, message, and details.

Node.js
res.[1]([2]).json({ code: [3], message: "Invalid data", details: "Missing fields" });
Drag options to blanks, or click blank then click option'
Astatus
B422
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of status.
Using string '422' instead of number 422 for the code property.