Node.js http.ClientRequest.connection Property
The http.ClientRequest.connection is an inbuilt application programming interface of class ClientRequest within the HTTP module which is used to get the reference of underlying client request socket.
Syntax:
const request.connection
Parameters: It does not accept any argument as the parameter.
Return Value: It does not return any value.
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);
const v = req.connection;
console.log( "request socket :- " + v)
process.exit(0)
});
|
Run the index.js file using the following command:
node index.js
Output:
request socket :- null
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.connection) {
console.log( "Requested for Connection" )
} else {
console.log( "Not Requested for Connection" )
}
process.exit(0)
});
|
Run the index.js file using the following command:
node index.js
Output:
Not Requested for Connection
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_connection
Last Updated :
18 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...