'req. 'connection' is @deprecated — since v13.0.0 - Use socket instead
I am trying to get the client's IP using Express:
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
When mouseover the req.connection in VS Code I get a warning:
(property) IncomingMessage.connection: Socket
@deprecated — since v13.0.0 - Use socket instead.
'connection' is deprecatedts(6385)
How to implement the 'socket instead' / what is the proper way to find the IP of the client?
Solution 1:[1]
You have just to replace req.connection.remoteAddress with req.socket.remoteAddress in the http package documentation request.connection is flagged as Deprecated and It's an alias of request.socket, you can directly use request.socket
request.socket is an instance of net.Socket you check all available methods here
Solution 2:[2]
Just type req.socket.remoteAddress instead and you are good to go!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yves Kipondo |
| Solution 2 | Shadman Fatin |
