How to get the user IP address in Meteor server?

前端 未结 6 815
情书的邮戳
情书的邮戳 2020-11-30 03:03

I would like to get the user IP address in my meteor application, on the server side, so that I can log the IP address with a bunch of things (for example: non-registered us

6条回答
  •  清歌不尽
    2020-11-30 03:56

    1 - Without a http request, in the functions you should be able to get the clientIP with:

    clientIP = this.connection.clientAddress;
    //EX: you declare a submitForm function with Meteor.methods and 
    //you call it from the client with Meteor.call().
    //In submitForm function you will have access to the client address as above
    

    2 - With a http request and using iron-router and its Router.map function:

    In the action function of the targeted route use:

    clientIp = this.request.connection.remoteAddress;
    

    3 - using Meteor.onConnection function:

    Meteor.onConnection(function(conn) {
        console.log(conn.clientAddress);
    });
    

提交回复
热议问题