码迷,mamicode.com
首页 > 其他好文 > 详细

koa2获取用户ip

时间:2018-11-05 13:37:57      阅读:2684      评论:0      收藏:0      [点我收藏+]

标签:soc   connect   func   cti   style   方法   用户   获取   address   

调用下面方法即可获取

// koa2 中 req 为 ctx.req
const getUserIp = (req) => {
  return req.headers[‘x-forwarded-for‘] ||
    req.connection.remoteAddress ||
    req.socket.remoteAddress ||
    req.connection.socket.remoteAddress;
}

websocket 连接时获取

ws.on(‘connection‘, function(wsocket) {
    const ip = wsocket._socket.remoteAddress;
})

 

koa2获取用户ip

标签:soc   connect   func   cti   style   方法   用户   获取   address   

原文地址:https://www.cnblogs.com/YMaster/p/9908503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!