Http2ServerRequest.url is a property of the Http2ServerRequest class that returns the request URL sent by the client. It provides the path and query string from the incoming HTTP/2 request, allowing the server to determine which resource or route was requested.
- Represents the URL associated with an incoming HTTP/2 request.
- Contains the request path along with the query string, if one is present.
- Is automatically populated for every request received by the server.
Syntax:
request.urlWhere,
- request: An instance of
Http2ServerRequest. - url: Property that returns the URL of the incoming request.
Return Value: This method returns the Request URL string.
Example 1:
// Node.js program to demonstrate the
// Http2ServerRequest.url 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 url of http2
// by using request.url method
const auth = request.url;
console.log( "request url :- " + auth)
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options,http2Handlers);
server.on('stream', (stream, requestheader) => {
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': '/name'});
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:
request url :- /name
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// Http2ServerRequest.url 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 url of http2
// by using request.url method
const auth = request.url;
response.end( "request url :- " + 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': '/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:
Received: request url :- /www.geeksforgeeks.org
client destroyed
server destroyed
Use Cases of Http2ServerRequest.url Property
- Routing incoming requests to different endpoints.
- Accessing query parameters from the request URL.
- Serving resources based on the requested path.
- Implementing custom URL-based request handling.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_request_url