Node.js http.ClientRequest.path Property
Last Updated :
05 Apr, 2023
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
const http = require( 'http' );
const PORT = process.env.PORT || 3000;
const httpServer = http.createServer(
function (request, response) {
const value = request.path;
console.log( "Request URL: " , request.url)
response.end( "request path : " + value, 'utf8' , () => {
console.log( "displaying the result..." );
httpServer.close(() => {
console.log( "server is closed" )
})
});
});
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
const http = require( 'http' );
const http2Handlers = (request, response) => {
const value = request.path;
response.end( "request path : " + value, 'utf8' , () => {
console.log( "displaying the result..." );
httpServer.close(() => {
console.log( "server is closed" )
})
});
};
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...