Using res.status to Send HTTP Status Codes in Express
📖 Scenario: You are building a simple Express server that responds to client requests. You want to send proper HTTP status codes to tell the client if the request was successful or if there was an error.
🎯 Goal: Build an Express route that uses res.status to send different HTTP status codes along with messages.
📋 What You'll Learn
Create an Express app with a GET route at
/statusAdd a variable
isSuccess to control the response statusUse
res.status to send 200 if isSuccess is trueUse
res.status to send 500 if isSuccess is falseSend a JSON message with a
message property describing the status💡 Why This Matters
🌍 Real World
Web servers use HTTP status codes to tell browsers or apps if requests worked or failed. This helps users understand what happened.
💼 Career
Backend developers must send correct status codes to build reliable APIs and web services.
Progress0 / 4 steps