0
0
Expressframework~30 mins

res.status for status codes in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
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 /status
Add a variable isSuccess to control the response status
Use res.status to send 200 if isSuccess is true
Use res.status to send 500 if isSuccess is false
Send 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
1
Set up Express app and route
Create an Express app by requiring express and calling express(). Then create a GET route at /status with a callback function that takes req and res as parameters.
Express
Need a hint?

Remember to require Express and create an app instance. Use app.get to define the route.

2
Add a variable to control success
Inside the /status route callback, create a variable called isSuccess and set it to true.
Express
Need a hint?

Just create a constant named isSuccess and assign it the value true.

3
Send status 200 or 500 based on isSuccess
Use an if statement to check if isSuccess is true. If yes, use res.status(200) and send a JSON object with { message: 'Success' }. Otherwise, use res.status(500) and send { message: 'Error' }.
Express
Need a hint?

Use res.status(code).json(object) to send status and JSON response.

4
Start the server on port 3000
Add code to start the Express server by calling app.listen on port 3000 with a callback that logs 'Server running on port 3000'.
Express
Need a hint?

Use app.listen(port, callback) to start the server and log a message.