0
0
Node.jsframework~10 mins

Setting response headers in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the Content-Type header to 'application/json'.

Node.js
res.setHeader('Content-Type', [1]);
Drag options to blanks, or click blank then click option'
A'application/xml'
B'text/html'
C'text/plain'
D'application/json'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/html' instead of 'application/json' for JSON data.
Forgetting to put the header value in quotes.
2fill in blank
medium

Complete the code to set the 'Cache-Control' header to 'no-cache'.

Node.js
res.setHeader('Cache-Control', [1]);
Drag options to blanks, or click blank then click option'
A'private'
B'public'
C'no-cache'
D'max-age=3600'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'max-age=3600' which allows caching for 1 hour.
Using 'public' which allows caching by any cache.
3fill in blank
hard

Fix the error in setting multiple headers by completing the method name.

Node.js
res.[1](200, {'Content-Type': 'text/html', 'X-Custom-Header': 'value'});
Drag options to blanks, or click blank then click option'
AwriteHead
BsetHeaders
CwriteHeaders
DsetHeader
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'setHeader' which only sets one header at a time.
Using non-existent methods like 'setHeaders' or 'writeHeaders'.
4fill in blank
hard

Fill both blanks to set the status code to 404 and the Content-Type header to 'text/plain'.

Node.js
res.[1](404, [2]);
Drag options to blanks, or click blank then click option'
AwriteHead
B{'Content-Type': 'text/plain'}
C{'Content-Type': 'application/json'}
DsetHeader
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'setHeader' which cannot set status code.
Passing headers as a string instead of an object.
5fill in blank
hard

Fill all three blanks to set status 200, Content-Type 'application/json', and a custom header 'X-Powered-By' with value 'NodeJS'.

Node.js
res.setHeader([3], 'NodeJS'); res.[1](200, [2]);
Drag options to blanks, or click blank then click option'
AwriteHead
B{'Content-Type': 'application/json'}
C'X-Powered-By'
DsetHeader
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to set all headers with setHeader only.
Passing header names without quotes.