Response headers tell the browser important information about the data it receives. Setting them helps control how the browser handles the response.
Setting response headers in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
response.setHeader(name, value);
name is the header name as a string, like 'Content-Type'.
value is the header value as a string, like 'application/json'.
Examples
Node.js
response.setHeader('Content-Type', 'text/html');
Node.js
response.setHeader('Cache-Control', 'no-cache');
Node.js
response.setHeader('Access-Control-Allow-Origin', '*');
Sample Program
This Node.js server sets two headers: it tells the browser the response is JSON and disables caching. Then it sends a JSON message.
Node.js
import http from 'node:http'; const server = http.createServer((request, response) => { response.setHeader('Content-Type', 'application/json'); response.setHeader('Cache-Control', 'no-store'); const data = { message: 'Hello, world!' }; response.end(JSON.stringify(data)); }); server.listen(3000, () => { console.log('Server running at http://localhost:3000/'); });
Important Notes
Headers must be set before sending the response body.
Header names are case-insensitive but usually written in standard capitalization.
Setting headers incorrectly can cause browsers to misinterpret your response.
Summary
Response headers give the browser important info about the response.
Use response.setHeader(name, value) to set headers in Node.js.
Always set headers before sending the response body.
Practice
1. What is the purpose of setting response headers in a Node.js server?
easy
Solution
Step 1: Understand what response headers do
Response headers tell the browser details about the response, like content type or caching rules.Step 2: Identify the correct purpose
Only To provide the browser with important information about the response describes this role correctly; other options describe unrelated server tasks.Final Answer:
To provide the browser with important information about the response -> Option BQuick Check:
Response headers = browser info [OK]
Hint: Headers tell browser about response before body [OK]
Common Mistakes:
- Thinking headers run code on server
- Confusing headers with server storage
- Believing headers create files
2. Which of the following is the correct syntax to set a response header named
Content-Type to application/json in Node.js?easy
Solution
Step 1: Recall Node.js method for setting headers
Node.js uses response.setHeader(name, value) to set headers.Step 2: Match syntax with options
Only response.setHeader('Content-Type', 'application/json'); uses setHeader correctly; others use incorrect method names.Final Answer:
response.setHeader('Content-Type', 'application/json'); -> Option AQuick Check:
Use setHeader(name, value) [OK]
Hint: Use setHeader method exactly as shown [OK]
Common Mistakes:
- Using header() instead of setHeader()
- Using set() or addHeader() which don't exist
- Wrong method name casing
3. What will be the value of the
Content-Type header sent to the client after running this code snippet?
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-Type', 'application/json');
res.end('{}');
});
server.listen(3000);medium
Solution
Step 1: Understand header overwriting behavior
Setting the same header twice overwrites the previous value in Node.js.Step 2: Identify final header value
The second setHeader call sets 'Content-Type' to 'application/json', replacing 'text/html'.Final Answer:
'application/json' -> Option DQuick Check:
Last setHeader call wins [OK]
Hint: Last setHeader call overwrites previous [OK]
Common Mistakes:
- Thinking both headers are sent
- Assuming first header stays
- Believing no header is sent
4. Identify the error in this Node.js code that tries to set a response header:
const http = require('http');
const server = http.createServer((req, res) => {
res.write('Hello');
res.setHeader('Content-Type', 'text/plain');
res.end();
});
server.listen(3000);medium
Solution
Step 1: Check order of header setting and response writing
Headers must be set before sending any part of the response body.Step 2: Identify the error in code order
res.write('Hello') sends body first, so setHeader after it causes error.Final Answer:
Headers must be set before writing the response body -> Option CQuick Check:
Set headers before body [OK]
Hint: Set headers before any res.write or res.end [OK]
Common Mistakes:
- Setting headers after writing body
- Thinking setHeader is invalid method
- Assuming res.end needs data always
5. You want to set multiple headers including
Content-Type as application/json and Cache-Control as no-cache in a Node.js server. Which code snippet correctly sets both headers before sending the response?hard
Solution
Step 1: Understand setting multiple headers at once
writeHead allows setting status and multiple headers before sending response.Step 2: Check order and correctness
res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'no-cache' }); res.end(JSON.stringify(data)); sets both headers before res.end, correctly using writeHead.Step 3: Identify errors in other options
res.setHeader({'Content-Type': 'application/json', 'Cache-Control': 'no-cache'}); res.end(JSON.stringify(data)); incorrectly passes an object to setHeader (expects name, value); B and C set headers after res.end (which sends headers).Final Answer:
res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'no-cache' }); res.end(JSON.stringify(data)); -> Option AQuick Check:
Use writeHead for multiple headers before response [OK]
Hint: Use writeHead to set many headers at once before response [OK]
Common Mistakes:
- Using setHeader with object (like Express)
- Setting headers after res.end
- Confusing setHeader and writeHead usage
