简体   繁体   中英

How do i get local ip address of a remote client with node.js using express framework?

I just ran into a task I need to get a local ip address of a remote client who is accessing a REST api on our server, I'm not using any load balancer (since I know nothing about it for now), my app is running locally on my pc and I'm exposing it to the internet with the tool ngrok, which hosts my app and makes it accessible to the internet. I'll share with you what I have done so far but this doesn't serve my need since it only gets the public ip address of the remote client, meaning that, if two different users are accessing API from same network, with this code, their ip addresses are reported alike,

RedisRouter.post('/incrementvisit', async (req: Request, res: Response, next: NextFunction) => {
try {
    console.log('req.ip', req.ip);
    console.log('request.connection.remoteAddress==================================', req.connection.remoteAddress);
    var ip = req.headers['x-forwarded-for'];
    console.log('ip from headers -------------->', ip);

    console.log('api after trimming', ip);
    res.send(await rs.visitCounter(req.body.name,ip));
} catch (error) {
    console.log(error);
    next(error);
}
});

`this always gives me same ip adress no for all thes users who are requesting api from the same network, which is their public ip address and not the local one. Basically, my need is to track the number of different users pinging our API, and i can't take same ip address to be accounted for two different persons requesting API from same network.

plus please suggest me some good beginner level reads on using web proxies and web networking for developers. Thank you , stay blessed.

我不确定哪个服务器ngrok正在运行,但是您是否尝试过:

var ip = req.headers["x-real-ip"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM