Skip to content
Related Articles

Related Articles

Node.js http.ClientRequest.path Property

View Discussion
Improve Article
Save Article
  • Last Updated : 09 Dec, 2020
View Discussion
Improve Article
Save Article

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 
var http = require('http'); 
    
// Setting up PORT 
const PORT = process.env.PORT || 3000; 
    
// Creating http Server 
var 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 
var 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 
var 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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!