0
0
Node.jsframework~5 mins

Setting response headers in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of setting response headers in Node.js?
Response headers provide metadata about the response, such as content type, caching rules, and security policies. They help the browser understand how to handle the response.
Click to reveal answer
beginner
How do you set a single response header in Node.js using the http module?
Use the response object's setHeader method, like: res.setHeader('Content-Type', 'application/json').
Click to reveal answer
intermediate
What happens if you try to set a header after the response has been sent?
Node.js will throw an error because headers must be set before sending the response body. Once headers are sent, they cannot be changed.
Click to reveal answer
intermediate
How can you set multiple headers at once in Node.js?
You can use res.writeHead(statusCode, headersObject) to set status and multiple headers together, for example: res.writeHead(200, {'Content-Type': 'text/html', 'Cache-Control': 'no-cache'}).
Click to reveal answer
beginner
Why is it important to set the 'Content-Type' header correctly?
It tells the browser what kind of data is being sent (like HTML, JSON, or plain text), so the browser can display or process it properly.
Click to reveal answer
Which method sets a single response header in Node.js?
Ares.headerSet()
Bres.getHeader()
Cres.sendHeader()
Dres.setHeader()
When must response headers be set in Node.js?
AAfter sending the response body
BBefore sending the response body
CAny time during the request
DOnly after the response ends
What does the 'Content-Type' header specify?
AThe type of data in the response
BThe size of the response
CThe server's IP address
DThe client's browser version
Which method sets multiple headers and status code at once?
Ares.writeHead()
Bres.setHeaders()
Cres.sendHeaders()
Dres.setStatus()
What happens if you set a header after calling res.end()?
AThe response restarts
BThe header is set successfully
CAn error occurs because headers are already sent
DThe header is ignored silently
Explain how to set response headers in Node.js and why it is important.
Think about how the browser knows what kind of data it receives.
You got /4 concepts.
    Describe the difference between res.setHeader() and res.writeHead() in Node.js.
    One is for single headers, the other for multiple headers and status.
    You got /4 concepts.