Node.js Http2ServerRequest.authority Method

Last Updated : 23 Jul, 2026

The http2ServerRequest.authority method in Node.js returns the authority value of an incoming HTTP/2 request. It represents the target host and optional port specified by the client.

  • It provides the target host and optional port.
  • It helps identify the requested server or virtual host.
  • It is useful when processing HTTP/2 requests.

Syntax:

request.authority

Where:

  • request: An instance of the Http2ServerRequest object.
  • authority: A read-only property that returns the authority (host and optional port) of the incoming HTTP/2 request.

Return Value: This method returns the string representation of the request authority pseudo-header field.

Example 1:

JavaScript
// Node.js program to demonstrate the
// Http2ServerRequest.authority method

const http2 = require('http2');
const fs = require('fs');

// Private key and public certificate for access
const options = {
  key: fs.readFileSync('private-key.pem'),
  cert: fs.readFileSync('public-cert.pem'),
};

// Request and response handler
const http2Handlers = (request, response) => {

  // Getting authority of http2 
  // by using request.authority method
  const auth = request.authority;
  console.log("authority :- " + auth)
};

// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options, http2Handlers);

server.on('stream', (stream, requestHeaders) => {
  stream.respond({ ':status': 200, 'content-type': 
  'text/plain' });
  stream.write('hello ');

  // Getting all information of this http2stream object
  // by using state method
  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');

const req = client.request({ 
  ':method': 'GET', ':path': '/' });

req.on('response', (responseHeaders) => {
  console.log("status : " 
  + responseHeaders[":status"]);
});

req.on('data', (data) => {
  console.log('Received: %s ',
  data.toString().replace(/(\n)/gm,""));
});

req.on('end', () => {
  client.close(() => {
    console.log("client destroyed");
  })
});

Output:

authority :- localhost:8000
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed

Example 2:

JavaScript
// Node.js program to demonstrate the
// Http2ServerRequest.authority method

const http2 = require('http2');
const fs = require('fs');

// Private key and public certificate for access
const options = {
  key: fs.readFileSync('private-key.pem'),
  cert: fs.readFileSync('public-cert.pem'),
};

// Request and response handler
const http2Handlers = (request, response) => {

  // Getting authority of http2 
  // by using request.authority method
  const auth = request.authority;
  response.end("authority :- " + auth)
};

// 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 method
  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': '/' });

req.on('data', (data) => {
  console.log('Received: %s ',
  data.toString().replace(/(\n)/gm,""));
});

req.on('end', () => {
  client.close(() => {
    console.log("client destroyed");
  })
});

Output:

Received: authority :- localhost:8000
client destroyed
server destroyed

Use Cases of http2ServerRequest.authority

  • Identifying the host requested by the client.
  • Supporting virtual host routing.
  • Validating the request's target authority.
  • Logging the requested host for monitoring or debugging.

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_request_authority

Comment

Explore