【问题标题】:get client ip in node.js [closed]在node.js中获取客户端IP [关闭]
【发布时间】:2019-12-17 06:15:39
【问题描述】:

我正在尝试使用以下代码控制日志客户端,但我的超级终端没有显示任何内容。

var getClientIp = function(req) {
return (req.headers["X-Forwarded-For"] ||
        req.headers["x-forwarded-for"] ||
        '').split(',')[0] ||
       req.client.remoteAddress;
}; //i replaces this section with different codes but nothing works

const express = require("express");
const app = express();
app.use(express.static(__dirname + '/'));
app.get("/", function(req,res){
     res.sendFile(__dirname + "/index.html");

var getClientIp = function(req) {
    return (req.headers["X-Forwarded-For"] ||
        req.headers["x-forwarded-for"] ||
        '').split(',')[0] ||
       req.client.remoteAddress;
};
onsole.log(getClientIp);});
app.listen(3000, function(){
console.log("server is started on port 3000");});

【问题讨论】:

  • 你的onsole.log 应该是console.log
  • 嗨,欢迎来到 StackOverflow。我们喜欢干净、有格式的问题!如果您想改进您的问题,请考虑格式化您的代码(并修复拼写错误),以便希望提供帮助的人可以更轻松地提供帮助!

标签: javascript node.js ip


【解决方案1】:
onsole.log(getClientIp);});

应该改为:

console.log(getClientIp(););

您将 getClientIp 定义为一个函数。如果您想实际获取 IP,则需要调用您的函数 --> () 另外,为什么您的日志条目中有一个右括号?对你来说最后一行... 删除“}”

【讨论】:

  • 我添加了 >> });因为我在获取请求回调函数中添加了 console.log(getClientIp();。感谢您的重播表明我的错误。
猜你喜欢
  • 2013-10-16
  • 2015-12-31
  • 2019-03-19
  • 1970-01-01
  • 2016-10-07
  • 2015-06-06
  • 2020-11-10
  • 2019-09-23
  • 2020-12-08
相关资源
最近更新 更多