The http.ClientRequest.reusedSocket is an inbuilt application programming interface of class ClientRequest within the HTTP module which is used to check if the request is sent through a reused socket.
Syntax:
const request.reusedSocket
Parameters: This property does not accept any argument as a parameter.
Return Value: This property returns the object of the client request-host.
Example 1: Filename-index.js
Javascript
const http = require( 'http' );
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type' : 'text/plain' });
res.end( 'okay' );
});
server.listen(3000, '127.0.0.1' , () => {
const options = {
port: 3000,
host: '127.0.0.1' ,
headers: {
'Connection' : 'Upgrade' ,
'Upgrade' : 'websocket'
}
};
const req = http.request(options);
req.host = '127.0.0.1'
const v = req.reusedSocket;
if (v)
console.log( "message is sent through reusedSocket" )
else
console.log( "message is not sent through reusedSocket" )
process.exit(0)
});
|
Run the index.js file using the following command:
node index.js
Output:
message is not sent through reusedSocket
Example 2: Filename-index.js
Javascript
const http = require( 'http' );
http.createServer((req, res) => { })
.listen(3000, '127.0.0.1' , () => {
const req = http.request({
port: 3000,
host: '127.0.0.1' ,
});
if (req.reusedSocket)
console.log( "message is sent through reusedSocket" )
else
console.log( "message is not sent through reusedSocket" )
process.exit(0)
});
|
Run the index.js file using the following command:
node index.js
Output:
message is not sent through reusedSocket
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_reusedsocket
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Jan, 2023
Like Article
Save Article