0
0
Expressframework~5 mins

res.set for response headers in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does res.set() do in Express?

res.set() sets HTTP response headers before sending the response to the client.

Click to reveal answer
beginner
How do you set multiple headers at once using res.set()?

You pass an object with key-value pairs where keys are header names and values are header values.

res.set({ 'Content-Type': 'application/json', 'Cache-Control': 'no-cache' })
Click to reveal answer
intermediate
Can you overwrite an existing header using res.set()?

Yes, calling res.set() with the same header name will overwrite the previous value.

Click to reveal answer
intermediate
What is the difference between res.set() and res.header() in Express?

They are aliases and behave the same way. Both set response headers.

Click to reveal answer
beginner
Why is setting response headers important in web development?

Headers control how browsers and clients handle the response, like content type, caching, security, and more.

Click to reveal answer
How do you set a single response header using res.set()?
Ares.set('text/html')
Bres.set({'Content-Type'})
Cres.set('Content-Type')
Dres.set('Content-Type', 'text/html')
What happens if you call res.set() twice with the same header name?
AThe header value is overwritten with the last call
BBoth values are sent as a list
CAn error is thrown
DThe first value is kept, the second ignored
Which of these is a valid way to set multiple headers at once?
Ares.set('X-Test', '123', 'X-Flag', 'true')
Bres.set({ 'X-Test': '123', 'X-Flag': 'true' })
Cres.set(['X-Test', '123'], ['X-Flag', 'true'])
Dres.set('X-Test:X-Flag', '123:true')
Is res.header() different from res.set() in Express?
AYes, <code>res.set()</code> only sets cookies
BYes, <code>res.header()</code> only reads headers
CNo, they are aliases and work the same
DYes, <code>res.header()</code> is deprecated
Why might you set the Cache-Control header using res.set()?
ATo control how browsers cache the response
BTo set the page title
CTo change the HTTP method
DTo redirect the user
Explain how to use res.set() to add headers to an Express response. Include examples for single and multiple headers.
Think about how you tell the browser what type of content you are sending or how to cache it.
You got /3 concepts.
    Why is setting response headers important in web applications? Give at least two reasons.
    Consider what happens if the browser does not know what kind of data it received.
    You got /4 concepts.