0
0
Node.jsframework~20 mins

Setting response headers in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Header Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A'application/json'
BBoth 'application/json' and 'text/html' headers are sent
C'text/html'
DNo 'Content-Type' header is sent
Attempts:
2 left
💡 Hint
Think about what happens when you set the same header twice in Node.js response.
component_behavior
intermediate
2: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?
Ares.writeHead(200, {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'});
Bres.headers = {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'};
C
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-cache');
Dres.setHeaders({'Content-Type': 'application/json', 'Cache-Control': 'no-cache'});
Attempts:
2 left
💡 Hint
Check the correct method names and usage for setting multiple headers at once.
🔧 Debug
advanced
2: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);
ABecause res.end must be called before setting headers.
BBecause res.setHeader is called after res.writeHead, headers set after writeHead are ignored.
CBecause the server is missing a call to res.flushHeaders() before res.end().
DBecause 'X-Custom-Header' is not a valid header name.
Attempts:
2 left
💡 Hint
Think about the order of setting headers and sending the response status line.
📝 Syntax
advanced
2: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?
Ares.setHeader('Set-Cookie', ['cookie1=value1; cookie2=value2']);
Bres.setHeader('Set-Cookie', 'cookie1=value1; cookie2=value2');
Cres.setHeader('Set-Cookie', 'cookie1=value1, cookie2=value2');
Dres.setHeader('Set-Cookie', ['cookie1=value1', 'cookie2=value2']);
Attempts:
2 left
💡 Hint
Check how Node.js expects multiple header values for the same header.
state_output
expert
3: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);
A{'Content-Type': 'text/plain', 'X-Test': 'third', 'Cache-Control': 'no-store'}
B{'Content-Type': 'text/plain', 'X-Test': 'second', 'Cache-Control': 'no-store'}
C{'Content-Type': 'text/plain', 'X-Test': 'first', 'Cache-Control': 'no-store'}
D{'Content-Type': 'text/plain', 'X-Test': 'second'}
Attempts:
2 left
💡 Hint
Consider the order of setHeader and writeHead calls and which headers take precedence.