Http in node, caller IP address

I need to have the ability to get the client IP address. On my flow i have http-in node that accept (get api call), i need to be able to get the client Ip address who called the API.

it is very urgent, appreciate your help

I am not certain, but since you say it is very urgent, feed the output of the node into a Debug node set to Show Complete Message and carefully inspect all the properties shown.

I have done this already and I didn't find any info that related to remote or client ip

The source IP is in the msg.req but it is hard to find if you are not familiar with that object as it is typically too large to completely show in the debug sidebar. Look up the ExpressJS request object online for details and work through each element to get to the data.

I think you'll get the full contents if you add a function node and in it call

console.log(msg.req);

You'll see the output in Node-RED log (not in debug sidebar).

finally the answer is (msg.req.ip), however it did not appear in the console.log(msg.req). I found it in theExpressJS request object online.

1 Like

I tried it myself and you are right. A lot of data is output but the ip is not there while it still works as msg.req.ip. Strange indeed. Thanks for the update.

I investigated this a bit and iterating the properties of msg.req using for (const item in msg.req) { does find msg.req.ip, however msg.req.hasOwnProperty("ip") returns false, which I assume means that req is derived from an object type that has this property. I guess that the debug output only shows OwnProperties of the object being shown.

Thanks for the info. is it possible to get the client port?

What do you mean by client port?

The IP and port that client use to send the request. now i can get the IP now.

The same way you found the IP, a quick internet search shows that the information in request can be retrieved also.

In a function node after your http in :

msg.remoteAddress = msg.req.connection.remoteAddress
msg.remotePort = msg.req.connection.remotePort
msg.localAddress = msg.req.connection.localAddress
msg.localPort = msg.req.connection.localPort
return msg;

use a complete msg debug node after the function to see the reply when a request is made.
ps .. what is the need for that .. isnt that localPort assigned by the OS and may change everytime ?

Thanks for the info

After reading a little bit more about Node.js req.connection, I think its better to replace connection with socket because connection is deprecated and may be removed in future Node js updates.

msg.req.socket.localPort

req.socket has the same effect.