📖 Scenario: You are building a simple Express server that sends back a greeting message. To make your response more informative and secure, you want to add custom HTTP headers.
🎯 Goal: Learn how to use res.set in Express to add custom response headers before sending the response.
📋 What You'll Learn
Create an Express app with a single GET route at '/'
Use res.set to add a custom header 'X-Custom-Header' with value 'HelloHeader'
Add another header 'Content-Type' with value 'text/plain'
Send a plain text response 'Welcome to the header demo!'
💡 Why This Matters
🌍 Real World
Web servers often need to send custom headers for security, caching, or metadata. Using <code>res.set</code> in Express helps control these headers easily.
💼 Career
Backend developers use Express to build APIs and web servers. Knowing how to set response headers is essential for controlling client-server communication.
Progress0 / 4 steps
1
Create the Express app and basic route
Create a variable called express that requires the 'express' module. Then create a variable called app by calling express(). Finally, add a GET route at path '/' with a callback function that takes req and res as parameters.
Express
Hint
Use require('express') to import Express and express() to create the app. Use app.get to define the route.
2
Add custom headers using res.set
Inside the GET route callback, use res.set to add a header 'X-Custom-Header' with value 'HelloHeader'. Also add a header 'Content-Type' with value 'text/plain'.
Express
Hint
Call res.set twice with the exact header names and values.
3
Send the plain text response
After setting the headers, use res.send to send the text 'Welcome to the header demo!' as the response body.
Express
Hint
Use res.send with the exact string to send the response.
4
Start the Express server
Add a line to start the server by calling app.listen on port 3000 with a callback function that logs 'Server running on port 3000'.
Express
Hint
Use app.listen with port 3000 and a callback that logs the message.
Practice
(1/5)
1. What does res.set do in an Express response?
easy
A. It sets or updates HTTP headers before sending the response.
B. It sends the response body to the client.
C. It closes the server connection immediately.
D. It logs request details to the console.
Solution
Step 1: Understand the purpose of res.set
res.set is used to add or modify HTTP headers in the response before sending it.
Step 2: Differentiate from other response methods
Sending the body is done by res.send, not res.set. Logging or closing connections are unrelated.
Final Answer:
It sets or updates HTTP headers before sending the response. -> Option A
Quick Check:
res.set sets headers [OK]
Hint: Remember: res.set changes headers, not body or connection [OK]
Common Mistakes:
Confusing res.set with res.send
Thinking res.set sends the response
Assuming res.set closes the connection
2. Which of the following is the correct syntax to set a single header using res.set?
easy
A. res.set('Content-Type', application/json);
B. res.set('Content-Type', 'application/json');
C. res.set({Content-Type: 'application/json'});
D. res.set('Content-Type' => 'application/json');
Solution
Step 1: Recall correct method signature
The correct syntax for setting one header is res.set(name, value) where both are strings.
A. Headers must be set before sending the response body.
B. res.set cannot set custom headers.
C. res.send should be called twice to set headers.
D. No error; code works fine.
Solution
Step 1: Check order of res.set and res.send
Headers must be set before calling res.send because sending the body finalizes the response.
Step 2: Identify the mistake
Here, res.send is called first, then res.set, which is too late to add headers.
Final Answer:
Headers must be set before sending the response body. -> Option A
Quick Check:
Set headers before send [OK]
Hint: Always set headers before sending response body [OK]
Common Mistakes:
Setting headers after res.send
Thinking res.set can't set custom headers
Calling res.send multiple times
5. You want to set multiple headers including Content-Type and Cache-Control in one call, but only if a condition is true. Which code correctly uses res.set with condition?
hard
A. res.set({'Content-Type': isJson ? 'application/json' : 'text/html'}); res.set('Cache-Control', 'no-store');
B. res.set('Content-Type', isJson ? 'application/json' : 'text/html', 'Cache-Control', 'no-store');
C. if (isJson) res.set({'Content-Type': 'application/json', 'Cache-Control': 'no-store'});
D. res.set('Content-Type', 'application/json' && 'Cache-Control', 'no-store');
Solution
Step 1: Understand how to set multiple headers conditionally
Using an object with res.set allows setting many headers at once. Wrapping in an if statement applies condition.
Step 2: Evaluate each option
if (isJson) res.set({'Content-Type': 'application/json', 'Cache-Control': 'no-store'}); correctly uses an if condition and sets multiple headers in one object. res.set('Content-Type', isJson ? 'application/json' : 'text/html', 'Cache-Control', 'no-store'); has invalid syntax with multiple pairs in one call. res.set({'Content-Type': isJson ? 'application/json' : 'text/html'}); res.set('Cache-Control', 'no-store'); splits headers into two calls but is valid; however, question asks for one call. res.set('Content-Type', 'application/json' && 'Cache-Control', 'no-store'); uses invalid logical operator in arguments.
Final Answer:
if (isJson) res.set({'Content-Type': 'application/json', 'Cache-Control': 'no-store'}); -> Option C
Quick Check:
if + res.set(object) for conditional multiple [OK]
Hint: Use object syntax inside if to set multiple headers [OK]
Common Mistakes:
Passing multiple header pairs as separate arguments
Using logical operators inside res.set arguments
Setting headers outside condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.