如何在nodejs模型中获取用户ip?并在我的表格列ip_address中更新

时间:2019-01-25 06:44:37

标签: node.js express

我想在模型中编写函数以获取用户ip,并且要存储在用户表中的节点js中具有列名ip_address。 (使用express,mysql)

  User.prototype.userip= function(req, res)
  {
    // here need access to IP address here
  }

2 个答案:

答案 0 :(得分:2)

req.connection.remoteAddress 

尝试一下

答案 1 :(得分:1)

我建议您使用os API from os to get IPv4

var os = require('os');
var networkInterfaces = os.networkInterfaces();

console.log( networkInterfaces );

API from npm ip to get IPv4

var ip = require("ip");
console.dir ( ip.address() );