0
0
Expressframework~5 mins

Error response formatting in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of formatting error responses in Express?
Formatting error responses helps clients understand what went wrong by providing clear, consistent messages and status codes.
Click to reveal answer
beginner
Which HTTP status code is commonly used for a generic server error in Express?
The status code 500 is used for internal server errors indicating something went wrong on the server side.
Click to reveal answer
beginner
How can you send a JSON error response with a message and status code in Express?
Use res.status(code).json({ error: 'message' }) to send a JSON error with the right HTTP status.
Click to reveal answer
intermediate
Why should error responses include both a status code and a message?
Status codes tell the client the type of error, while messages explain details, making debugging easier.
Click to reveal answer
intermediate
What is a common pattern to handle errors globally in an Express app?
Use an error-handling middleware function with four arguments (err, req, res, next) to catch and format errors consistently.
Click to reveal answer
Which method sets the HTTP status code in an Express response?
Ares.status()
Bres.sendStatus()
Cres.json()
Dres.setStatus()
What is the correct way to send a JSON error message with status 404 in Express?
Ares.sendStatus(404, { error: 'Not found' })
Bres.json(404, { error: 'Not found' })
Cres.status(404).json({ error: 'Not found' })
Dres.error(404, 'Not found')
In Express, what does an error-handling middleware function require?
ANo parameters
BFour parameters: err, req, res, next
CTwo parameters: req, res
DThree parameters: req, res, next
Why is it important to send consistent error response formats?
ATo help clients parse and handle errors easily
BTo make the server faster
CTo reduce the size of responses
DTo avoid using status codes
Which status code indicates a client error due to bad input?
A500
B302
C200
D400
Explain how to create a global error handler in Express and why it is useful.
Think about middleware that catches errors and sends a response.
You got /4 concepts.
    Describe the key parts of a well-formatted error response in an Express app.
    What does the client need to understand the error?
    You got /4 concepts.