nodejs get client ip

1 nodejs

//传入请求HttpRequest
function getClientIp(req) {
        return req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
}

 

2、express

// REQ: request the HttpRequest 
req.ip

 

The above two methods to obtain the IP format ::: ffff: 127.0.0.1,

const ip = '::ffff:127.0.0.1'.match(/\d+\.\d+\.\d+\.\d+/)[0]
console.log(ip)

 

Also can refer to: How A to the Determine the User's IP address in the Node

Guess you like

Origin www.cnblogs.com/xy-ouyang/p/12162949.html