Discover how a few simple methods can save you from confusing bugs and frustrated users!
Why Response methods and status codes in Node.js? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a web server that sends back messages to users. You write code to send plain text responses and manually add numbers like 200 or 404 to tell if things worked or not.
Manually setting response codes and messages is tricky and easy to mess up. You might forget to set the right code, confuse users, or make your server send wrong info. This causes bugs and bad user experience.
Response methods and status codes in Node.js web frameworks like Express let you send clear, correct replies easily. You just call simple methods to set status codes and send data, so your server talks clearly with browsers and apps.
res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Success');
res.status(200).send('Success');
This makes your server responses clear, consistent, and easy to manage, improving communication with users and other apps.
When a user logs in, your server sends a 200 status with a welcome message if successful, or a 401 status if the password is wrong, so the app knows exactly what happened.
Manual response handling is error-prone and confusing.
Response methods simplify sending correct status codes and messages.
Clear responses improve user experience and app reliability.
Practice
res.status(404) method do in a Node.js response?Solution
Step 1: Understand the purpose of
This method sets the HTTP status code that tells the browser what happened with the request.res.status()Step 2: Identify the meaning of 404 status code
Status code 404 means 'Not Found', indicating the requested resource does not exist.Final Answer:
Sets the HTTP status code to 404 (Not Found) for the response -> Option CQuick Check:
Status code 404 = Not Found [OK]
- Thinking res.status sends the response body
- Confusing status code with redirect
- Assuming res.status ends the response
Solution
Step 1: Recall Express response chaining
Useres.status(code)to set status, then calljson()to send JSON data.Step 2: Check each option's syntax
res.status(200).json({ message: 'OK' }) correctly chainsstatus(200)andjson(). Others misuse method order or names.Final Answer:
res.status(200).json({ message: 'OK' }) -> Option AQuick Check:
Set status then send JSON = res.status(200).json({ message: 'OK' }) [OK]
- Calling send before status
- Using non-existent sendJson method
- Passing status code inside json()
app.get('/test', (req, res) => {
res.status(201).send('Created');
});Solution
Step 1: Understand status and send chaining
res.status(201)sets status code 201 (Created), thensend('Created')sends that text as the response body.Step 2: Confirm behavior of Express response
Express allows chaining status and send; the response will have status 201 and body 'Created'.Final Answer:
A response with status 201 and body 'Created' -> Option BQuick Check:
Status 201 + send text = A response with status 201 and body 'Created' [OK]
- Assuming default status 200 is sent
- Thinking send() clears status
- Believing chaining status and send causes error
app.get('/redirect', (req, res) => {
res.send('Redirecting...');
res.redirect('/new-url');
});Solution
Step 1: Understand response flow in Express
Onceres.send()is called, the response is sent and closed.Step 2: Check order of methods
Callingres.redirect()afterres.send()tries to send headers again, causing an error.Final Answer:
Calling res.redirect after res.send causes an error -> Option AQuick Check:
Send ends response; redirect after send fails [OK]
- Thinking order doesn't matter
- Believing send only works with JSON
- Assuming no error on multiple sends
Solution
Step 1: Set the status code correctly
Useres.status(400)to set the HTTP status to 400 (Bad Request).Step 2: Send JSON response
Usejson()to send the JSON object with the error message.Step 3: Check incorrect options
res.json(400, { error: 'Invalid input' }) uses wrong method signature (sends 400 as JSON body with status 200); res.status(400).send('Invalid input') sends plain text string (Content-Type text/html) instead of JSON; res.sendStatus(400).json({ error: 'Invalid input' }) ends response with sendStatus before json.Final Answer:
res.status(400).json({ error: 'Invalid input' }) -> Option DQuick Check:
Status then json() sends JSON with code [OK]
- Passing status code inside json()
- Using send instead of json for JSON data
- Calling sendStatus before json
