How to get user's IP address in a bot with node.js?

主宰稳场 提交于 2021-02-17 05:28:08

问题


I am writing a bot in node.js. May I know how can I insert code to console.log the IP address of message sender please? I need the IP address to perform some auto-log in.

Many Thanks!!


回答1:


You can get ip address from request object by

request.connection.remoteAddress




回答2:


To get the Ip address of your User

    var app = require('express')();

    app.get("/ip", (req, res) => {
      console.log(req.ip) // "::ffff:127.0.0.1" ::ffff: is a subnet prefix for IPv4 (32 bit) 
      let ip = req.ip.split(':');
      console.log(ip[3]);
       res.json(ip[3]);  // ==> 127.0.0.1 You can Get Your Ip address only 
    });
app.listen(3003);



回答3:


Just want to let you know that the thing that worked for me was this:

server.post('/api/messages', [
    function(req,res,next){ 
        console.log(req.connection.remoteAddress); 
        next();        
    },
    connector.listen()
]); 

Source: https://github.com/Microsoft/BotBuilder/issues/3316



来源:https://stackoverflow.com/questions/42452028/how-to-get-users-ip-address-in-a-bot-with-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!