Express.js req.ip is returning ::ffff:127.0.0.1

前端 未结 8 717
谎友^
谎友^ 2020-12-02 06:37

I am currently trying to get the IP of the requested user. The problem is the IP is returning ::ffff:127.0.0.1 instead of 127.0.0.1. I tried using

相关标签:
8条回答
  • 2020-12-02 07:25

    You can Get your Ip address alone or with The specified family using sockets

         var app = require('express')();
    
     app.get("/ip", (req, res) => {
            console.log(req.ip) 
           let ip = req.ip.split(':');
            let ip_details = req.socket.address();
              console.log(ip_details);                     
       // { address: '::ffff:127.0.0.1', family: 'IPv6', port: 3001 
    
               console.log(ip[3]);//127.0.0.1
                                res.json(ip[3]);  
          }
    
    0 讨论(0)
  • 2020-12-02 07:28

    Windows 7 has IPv6 enabled by default. Even though my server listens on IPv4 only, Windows 7 sends the ::ffff: prefix to the IPv4 as part of the transition to IPv6

    ::ffff:0:0:0/96 — A prefix used for IPv4-translated addresses which are used by the Stateless IP/ICMP Translation (SIIT) protocol.

    Transition from IPv4

    0 讨论(0)
提交回复
热议问题