Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Response Methods and Status Codes in Node.js
📖 Scenario: You are building a simple web server using Node.js that responds to client requests with different messages and status codes.This is like a shopkeeper who greets customers differently depending on their request and tells them if something is okay or if there is a problem.
🎯 Goal: Create a Node.js server that uses response methods to send messages and status codes to the client.You will learn how to set status codes and send text responses properly.
📋 What You'll Learn
Create a basic HTTP server using Node.js
Use res.statusCode to set HTTP status codes
Use res.end() to send responses
Send different messages for different URL paths
Use correct status codes for success and error
💡 Why This Matters
🌍 Real World
Web servers use status codes and response methods to tell browsers if a page loaded successfully or if there was an error.
💼 Career
Understanding how to send correct status codes and responses is essential for backend developers working with Node.js and building APIs.
Progress0 / 4 steps
1
Create a basic HTTP server
Create a Node.js HTTP server using http.createServer and assign it to a variable called server. The server should listen on port 3000.
Node.js
Hint
Use const server = http.createServer((req, res) => { }) to create the server.
Then call server.listen(3000) to start it.
2
Set status code for root URL
Inside the server callback, add an if statement to check if req.url is '/'. If yes, set res.statusCode to 200.
Node.js
Hint
Use if (req.url === '/') to check the URL.
Set res.statusCode = 200 inside the block.
3
Send response message for root URL
Inside the if block for req.url === '/', send the text response 'Welcome to the homepage!' using res.end().
Node.js
Hint
Use res.end('Welcome to the homepage!') to send the message and end the response.
4
Handle unknown URLs with 404 status
Add an else block after the if for req.url === '/'. Inside it, set res.statusCode to 404 and send the response 'Page not found' using res.end().
Node.js
Hint
Use else after the if block.
Inside else, set res.statusCode = 404 and call res.end('Page not found').
Practice
(1/5)
1. What does the res.status(404) method do in a Node.js response?
easy
A. Redirects the client to a page named '404'
B. Sends the response body with the text '404'
C. Sets the HTTP status code to 404 (Not Found) for the response
D. Ends the response without sending any data
Solution
Step 1: Understand the purpose of res.status()
This method sets the HTTP status code that tells the browser what happened with the request.
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 C
Quick Check:
Status code 404 = Not Found [OK]
Hint: Status codes tell browser what happened; 404 means not found [OK]
Common Mistakes:
Thinking res.status sends the response body
Confusing status code with redirect
Assuming res.status ends the response
2. Which of the following is the correct way to send a JSON response with status 200 in Express?
easy
A. res.status(200).json({ message: 'OK' })
B. res.send(200).json({ message: 'OK' })
C. res.json(200, { message: 'OK' })
D. res.status(200).sendJson({ message: 'OK' })
Solution
Step 1: Recall Express response chaining
Use res.status(code) to set status, then call json() to send JSON data.
Step 2: Check each option's syntax
res.status(200).json({ message: 'OK' }) correctly chains status(200) and json(). Others misuse method order or names.
Final Answer:
res.status(200).json({ message: 'OK' }) -> Option A
Quick Check:
Set status then send JSON = res.status(200).json({ message: 'OK' }) [OK]
Hint: Set status first, then call json() to send data [OK]
Common Mistakes:
Calling send before status
Using non-existent sendJson method
Passing status code inside json()
3. What will the following Express code send to the client?
A. Calling res.redirect after res.send causes an error
B. res.redirect should be called before res.send
C. res.send cannot send strings, only JSON
D. No error; this code works fine
Solution
Step 1: Understand response flow in Express
Once res.send() is called, the response is sent and closed.
Step 2: Check order of methods
Calling res.redirect() after res.send() tries to send headers again, causing an error.
Final Answer:
Calling res.redirect after res.send causes an error -> Option A
Quick Check:
Send ends response; redirect after send fails [OK]
Hint: Send or redirect ends response; don't call both [OK]
Common Mistakes:
Thinking order doesn't matter
Believing send only works with JSON
Assuming no error on multiple sends
5. You want to send a JSON response with status 400 and a message 'Invalid input'. Which code correctly does this in Express?
hard
A. res.json(400, { error: 'Invalid input' })
B. res.sendStatus(400).json({ error: 'Invalid input' })
C. res.status(400).send('Invalid input')
D. res.status(400).json({ error: 'Invalid input' })
Solution
Step 1: Set the status code correctly
Use res.status(400) to set the HTTP status to 400 (Bad Request).
Step 2: Send JSON response
Use json() 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 D
Quick Check:
Status then json() sends JSON with code [OK]
Hint: Use status() then json() to send JSON with status [OK]