简体   繁体   English

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

[英]How to get user ip in nodejs model ? and update in my table column ip_address

I want to write function in models to get user ip , and want to store in user table has column name ip_address in node js. 我想在模型中编写函数以获取用户ip,并希望存储在用户表中的节点js中具有列名ip_address。 (using express, mysql) (使用express,mysql)

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

尝试这个

I recommend you to use os API from os to get IPv4 我建议您使用来自os os API来获取IPv4

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

console.log( networkInterfaces );

API from npm ip to get IPv4 来自npm ip API获取IPv4

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM