I am using req.connection.remoteAddress to try to get the ip address from the client that do the request but I just get something like this ::ffff:10.1.86.195.
I think what I got above is just an ipv4 of my nodejs app, right ?
I need to get the real ipv6 address from the client that do the request to make nodejs app.
Any suggestion ?
Related
I'm noob with node.js but I know node.js is a server side language so when we make an http request we probably use the server IP address, right ?
I want to know if there any solution to use the client IP address when I wan to make an http request ?
Sorry for my bad English.
Thanks
I'm presuming you're trying to make an http request to your node server. If so, then it's not possible because your client has their own IP address which is distinct from that of the server.
I am using the ip library of npm.
I have two config files, one for React and one for Node, for the same application.
const ip = require('ip');
console.log(ip.address());
This returns different ip addresses for the React config file(inside the src folder-127.0.0.1) and Node server file(outside the src folder - IPv4 address).
The issue is that I am pretty sure that I ran the exact same code earlier and it gave me the same ip addresses for both as then I was able to access my webpages. I need the same ip to make requests to my node backend, I cannot afford it in production. Are there other definite methods of doing this?
You get different ip results because 2 method call ip.address() are using different network interfaces.
To make ip.address() return identical result, you can pass network interface name as the first parameter, such as en0:
const ip = require('ip');
console.log(ip.address('en0'));
p.s. To get all current networks interface names, os.networkInterfaces() can be used.
Update: OP try to get IP address in React code, in browser side. This is mission impossible. Otherwise, it would bring huge security problem.
Update 2: OP don't want to store endpoint IP address in frontend code for security reason, neither want to retrieve the IP address first (network overhead issue). In this case, you can make a proxy in server. All frontend know is interacting with current server, the data exchange is delivered by server as:
Browser <--> Server <--> Various endpoint IP
The steps are:
The server (that host the frontend code) get request from browser
Server check which endpoint would be used for that client
Server send the request to specific endpoint
Server get response from endpoint
Server return the response in above step to browser
How can I put the server IP address when I connect to the server in the frontend?
var socket = io.connect('http://localhost:3000/panel');
So instead localhost put the correspondent server IP address for the production environment.
Thanks.
Since this Javascript is executing in the client, you can just do the following:
document.location.hostname contains the hostname, and document.location.port the port. It's as easy as that.
I am using socket.io on heroku, and on socket connection I would like to get the client ip,currently I am getting an IP which is not the real IP (I think this is heroku's ip).
I am using socket.manager.handshaken[socket.id].address.address to get the user's ip.. do you have another way?
Try:
socket.manager.handshaken[socket.id].remoteAddress
If you need the client port then that would be:
socket.manager.handshaken[socket.id].remotePort
Is it possible to get the client's ip address on clientside (within the browser) when using socket.io? Using pure Javascript alone can't get the client's IP address.
There is no way, unless you send the IP address over the socket from the server side.