Challenge - 5 Problems
Response Header Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Node.js HTTP response header code?
Consider the following Node.js code snippet using the native HTTP module. What will be the value of the 'Content-Type' header sent to the client?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'text/html'); res.end(); }); server.listen(3000);
Attempts:
2 left
💡 Hint
Think about what happens when you set the same header twice in Node.js response.
✗ Incorrect
In Node.js, setting the same header multiple times with res.setHeader overwrites the previous value. So the last setHeader call determines the header value sent.
❓ component_behavior
intermediate2:00remaining
Which option correctly sets multiple headers in a Node.js HTTP response?
You want to set both 'Content-Type' to 'application/json' and 'Cache-Control' to 'no-cache' in a Node.js HTTP response. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
Check the correct method names and usage for setting multiple headers at once.
✗ Incorrect
The writeHead method can set status code and multiple headers at once. setHeaders is not a valid method, and directly assigning to res.headers does not work.
🔧 Debug
advanced2:00remaining
Why does this Node.js code fail to send the custom header?
Examine the code below. Why is the 'X-Custom-Header' not visible in the response headers?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.setHeader('X-Custom-Header', '12345'); res.end('Hello'); }); server.listen(3000);
Attempts:
2 left
💡 Hint
Think about the order of setting headers and sending the response status line.
✗ Incorrect
In Node.js, res.writeHead sends the status line and headers immediately. Any headers set after writeHead are ignored.
📝 Syntax
advanced2:00remaining
Which option correctly sets a header with multiple values in Node.js?
You want to set the 'Set-Cookie' header with two cookies in a Node.js HTTP response. Which code snippet is syntactically correct and sends both cookies?
Attempts:
2 left
💡 Hint
Check how Node.js expects multiple header values for the same header.
✗ Incorrect
Node.js allows setting multiple headers with the same name by passing an array of strings. Using a single string with commas or semicolons does not correctly set multiple cookies.
❓ state_output
expert3:00remaining
What headers are sent by this Node.js HTTP server code?
Analyze the following code. Which headers will the client receive in the response?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { res.setHeader('Content-Type', 'text/plain'); res.setHeader('X-Test', 'first'); res.setHeader('X-Test', 'second'); res.writeHead(200, {'X-Test': 'third', 'Cache-Control': 'no-store'}); res.end('Done'); }); server.listen(3000);
Attempts:
2 left
💡 Hint
Consider the order of setHeader and writeHead calls and which headers take precedence.
✗ Incorrect
writeHead sends headers immediately and overwrites any previously set headers with the same name. So 'X-Test' is set to 'third' from writeHead, and 'Cache-Control' is added. 'Content-Type' remains from setHeader.