Node.js request.socket Property - GeeksforGeeks

01 Sep, 2020
The request.socket (Added in v0.3.0) property is an inbuilt property of the ‘http’ module which references to the underlying socket and most users don’t get access to this property. Particularly, the socket doesn’t emit ‘readable‘ events but, the socket could be accessed via request.connection. This property guarantees to being an instance of the <net.Socket> class, a subclass of <stream.Duplex>.
In order to get a response and a proper result, we need to import ‘http’ module.
const http = require('http'); 
Syntax:
request.socket

Parameters: This property does not accept any parameters.
Return Value: It returns the request data in the form of an object which contains a huge amount of data related to socket.
<stream.Duplex>:  <stream.Duplex> or duplex stream is a stream that implements both a readable and a writable.
The below examples illustrate the use of request.socket property in Node.js.
Example 1:  Filename: index.js
// Node.js program to demonstrate the // req.socket property   // Using require to access http module const http = require('http');  // Requesting from google serverconst req = http.get({ host: 'www.geeksforgeeks.org' });  // Ending the requestreq.end();  req.once('response', (res) => {      // Printing socket after getting response    console.log(req.socket);      // Printing address and port after    // getting response    console.log(`IP address of geeksforgeeks is             ${req.socket.localAddress}.`);                  console.log(`Its Port is ${req.socket.localPort}.`);});
Output:
>> <ref *1> Socket{ connecting: false,
    _hadError: false,
    _parent: null,
    _host: ‘www.geeeksforgeeks.org’... [Symbol(kBytesWritten)]: 0 }
>> IP address is 192.168.43.207
>> Its port is 56933.
Example 2: Filename: index.js
// Node.js program to demonstrate the // req.socket property   // Using require to access http module const { get } = require('http');  // Setting host server urlconst options = { host: 'www.geeksforgeeks.org' };  // Requesting from geeksforgeeks serverconst req = get(options);req.end();  req.once('response', (res) => {      // Printing the requestrelated data    console.log("Status:", res.statusCode,                         res.statusMessage);      console.log("Host:", req.socket._host);      console.log("Method:",         req.socket.parser.outgoing.method);      console.log("Parser Header:",             req.socket.parser.outgoing._header);      console.log("Writable:", req.socket.writable);    console.log("Readable:", req.socket.readable);          console.log("Http Header:",             req.socket._httpMessage._header);      if (req.socket._httpMessage._header ===             req.socket.parser.outgoing._header) {        console.log("Both headers are exactly same...")    } else {        console.log("Headers are not same...")    }      // Printing address and port after    // getting response    console.log(`IP address of geeksforgeeks is         ${req.socket.localAddress}.`);      console.log(`Its port is ${req.socket.localPort}.`);});
Run index.js file using the following command:
node index.js

Output:
>> Status: 301 Moved Permanently
>> Host: www.geeksforgeeks.org
>> Method: GET
>> Parser Header: GET / HTTP/1.1
    Host: www.geeksforgeeks.org
    Connection: close
>> Writable: true
>> Readable: true
>> Http Header: GET / HTTP/1.1
    Host: www.geeksforgeeks.org
    Connection: close
>> Both headers are exactly same...
>> IP address is 192.168.43.207
>> Its port is 57425.
Reference: https://nodejs.org/api/http.html#http_request_socket
Node.js-Misc
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
JWT Authentication with Node.js
Express.js req.params Property
Difference between npm i and npm ci in Node.js
Mongoose Populate() Method
Roadmap to Become a Web Developer in 2022
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
Convert a string to an integer in JavaScript