expressjsHow do I set a header using Express.js?
Express.js is a web application framework for Node.js. It is used to create web applications and APIs. To set a header using Express.js, use the set
method of the response
object.
Example code
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.set('Content-Type', 'text/html');
res.send('<h1>Hello World</h1>');
});
app.listen(3000);
Output example
Server listening on port 3000
The code above sets the response header to Content-Type: text/html
. This tells the browser to render the response as an HTML page.
The set
method takes two parameters: the header name and the header value. It is also possible to set multiple headers at once using the set
method.
Example
res.set({
'Content-Type': 'text/html',
'X-Powered-By': 'Express'
});
Helpful links
More of Expressjs
- How do I find Express.js tutorials on YouTube?
- How do I set the time zone in Express.js?
- How can I use an ExpressJS webhook to receive data from an external source?
- How can I set up X-Frame-Options in ExpressJS?
- How can I use the x-forwarded-for header in Express.js?
- How can I use Express.js and Winston together to create a logging system?
- How do Express.js and Fastify compare in terms of performance and scalability?
- How do I write unit tests for ExpressJS?
- How do I manage user roles in Express.js?
- How can I use express-zip js to zip and download files?
See more codes...