🚀 Express.js
Beginner
How do you set response headers in Express.js?
Answer
Set individual headers with res.set('Header-Name', 'value') or res.setHeader(). Set multiple headers at once: res.set({ 'Content-Type': 'application/json', 'X-Custom': 'value' }). Use res.type('json') as a shorthand for Content-Type. Read headers from the response with res.get('Header-Name'). For security, set headers like X-Content-Type-Options, X-Frame-Options, and Content-Security-Policy — or use the helmet package which sets all important security headers automatically. Note that headers must be set before calling res.send() or res.json() — they cannot be set after the response is sent.
Previous
What is a REST API?
Next
What is the difference between app.listen() and http.createServer()?