The Http2ServerResponse.setHeader() method in Node.js is used to set a response header before sending an HTTP/2 response to the client. It allows you to define header fields such as content type, caching rules, or custom headers.
- It sets a header for the HTTP/2 response.
- It adds or updates a response header field.
- It must be called before the response headers are sent.
Syntax
response.setHeader(name, value)Where:
- name: Name of the header.
- value: Value of the header.
Return Value: The Http2ServerResponse object, allowing method chaining.
Example 1:
// Node.js program to demonstrate the
// Http2ServerResponse.setHeader() method
// Importing the http2 module
const http2 = require('http2');
// Importing the fs module
const fs = require('fs');
// Private key and public certificate for access
const options = {
// Reading the file private.key.pem file synchronously
key: fs.readFileSync('private-key.pem'),
// Reading the file public.cert.pem file synchronously
cert: fs.readFileSync('public-cert.pem'),
};
// Request and response handler
const http2Handlers = (request, response) => {
// Setting new header by using response.setHeader()
response.setHeader('Content-Type', 'hello world; charset=utf-8');
// Getting header's object by using getHeaders() method
const value = response.getHeaders()
// Display the header
console.log(value);
};
// Creating and Initializing server by
// using http2.createServer() method
const server = http2.createServer(options,http2Handlers);
server.on('stream', (stream, requestheader) => {
stream.write('hello ');
// Getting all information of this http2stream
// object by using state api
const status = stream.state;
stream.end("priority weight : " + status.weight);
// Stopping the server by
// using the close() method
server.close(()=>{
console.log("server destroyed");
})
});
server.listen(8000);
// Creating and initializing client by
// using tls.connect() method
const client = http2.connect('http://localhost:8000');
// Handling the request
const req = client.request({ ':method': 'GET', ':path': '/'});
req.on('response', (responsesocket) => {
console.log("status : " + responsesocket[":status"]);
});
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(()=>{
console.log("client destroyed");
})
});
Output
{'content-type': 'hello world; charset=utf-8'}
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// Http2ServerResponse.setHeader() method
// Importing http2 module
const http2 = require('http2');
// Importing fs module
const fs = require('fs');
// Private key and public certificate for access
const options = {
// Reading the private-key.pem file synchronously
key: fs.readFileSync('private-key.pem'),
// Reading the public-cert.pem file synchronously
cert: fs.readFileSync('public-cert.pem'),
};
// Request and response handler
const http2Handlers = (request, response) => {
// Setting new header
response.setHeader('Foo', 'bar');
response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
// Getting header's object
// by using getHeaders() method
const value = response.getHeaders()
// Display the header
console.log(value);
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options, http2Handlers);
server.on('stream', (stream, requestHeaders) => {
// Getting all information of this http2stream object
// by using state api
const status = stream.state;
stream.end("The sum weight of all Http2Stream : "
+ status.sumDependencyWeight);
// Stopping the server
// by using the close() method
server.close(()=>{
console.log("server destroyed");
})
});
server.listen(8000);
// Creating and initializing client
// by using tls.connect() method
const client = http2.connect('http://localhost:8000');
const req = client.request({
':method': 'GET', ':path': '/www.geeksforgeeks.org' });
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(()=>{
console.log("client destroyed");
})
});
Output:
{ foo: 'bar', 'set-cookie': [ 'foo=bar', 'bar=baz' ] }
Received: The sum weight of all Http2Stream : 0
client destroyed
server destroyed
Use Cases of Http2ServerResponse.setHeader()
- It is used to set the content type of an HTTP/2 response.
- It is used to add custom headers before sending a response.
- It is used to configure caching behavior through response headers.
- It is used to send security-related headers to the client.