nodejs获取客户端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: 请求HttpRequest req.ip
上面两种方式获取的IP格式:::ffff:127.0.0.1,
const ip = '::ffff:127.0.0.1'.match(/\d+\.\d+\.\d+\.\d+/)[0]
console.log(ip)
posted on 2020-01-07 17:49 wenbin_ouyang 阅读(1053) 评论(0) 编辑 收藏 举报