koa2获取用户ip

调用下面方法即可获取

// 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;
})

 

转载于:https://www.cnblogs.com/YMaster/p/9908503.html

    原文作者:SHY15651907150
    原文地址: https://blog.csdn.net/SHY15651907150/article/details/101747667
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞