Setting Response Headers with res.set in Express
📖 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