Node.js http.ClientRequest.path Property
The http.ClientRequest.path is an inbuilt application programming interface of class ClientRequest within HTTP module which is used to get the request path for the particular client request.
Syntax:
request.path
Parameters: This property does not accept any argument as a parameter.
Return Value: This property returns the request path for the particular client request.
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the // request.path property // Importing http module const http = require( 'http' ); // Setting up PORT const PORT = process.env.PORT || 3000; // Creating http Server const httpServer = http.createServer( function (request, response) { // Getting request path // by using request.path const value = request.path; console.log( "Request URL: " , request.url) // Display result response.end( "request path : " + value, 'utf8' , () => { console.log( "displaying the result..." ); httpServer.close(() => { console.log( "server is closed" ) }) }); }); // Listening to http Server httpServer.listen(PORT, () => { console.log( "Server is running at port 3000..." ); }); |
Run the index.js file using the following command:
node index.js
Output:
Server is running at port 3000...
Now open your browser and go to http://localhost:3000/, you will see the following output on your screen:
request path : undefined
Now close the browser and you will see the following output on your terminal screen:
Server is running at port 3000... Request URL: / displaying the result... Request URL: /favicon.ico displaying the result...
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the // request.path property // Importing http module const http = require( 'http' ); // Request and response handler const http2Handlers = (request, response) => { // Getting request path // by using request.path const value = request.path; // display result response.end( "request path : " + value, 'utf8' , () => { console.log( "displaying the result..." ); httpServer.close(() => { console.log( "server is closed" ) }) }); }; // Creating http Server const httpServer = http.createServer(http2Handlers) .listen(3000, () => { console.log( "Server is running at port 3000..." ); }); |
Run the index.js file using the following command:
node index.js
Output:
Server is running at port 3000...
Now open your browser and go to http://localhost:3000/, you will see the following output on your screen:
request path : undefined
Now close the browser and you will see the following output on your terminal screen:
Server is running at port 3000... displaying the result... server is closed
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_path
Contact Us