Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在node.js中获取客户端ip_Javascript_Node.js_Ip - Fatal编程技术网

Javascript 在node.js中获取客户端ip

Javascript 在node.js中获取客户端ip,javascript,node.js,ip,Javascript,Node.js,Ip,我正在尝试使用下面的代码来控制台日志客户端I,但我的超级终端没有显示任何内容 var getClientIp = function(req) { return (req.headers["X-Forwarded-For"] || req.headers["x-forwarded-for"] || '').split(',')[0] || req.client.remoteAddress; }; //i replaces this section wi

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

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");});
应改为:

console.log(getClientIp(););
您将getClientIp定义为一个函数。如果要实际获取IP,需要调用函数-->() 还有,为什么日志条目中有一个结束括号?最后一行也一样。。。
删除“}”

你的
onsole.log
应该是
console.log
Hi,欢迎使用StackOverflow。我们喜欢干净、格式化的问题!如果您想改进您的问题,请考虑格式化您的代码(并修复拼写错误),以便希望提供帮助的人可以更轻松地提供帮助!我添加了>>});因为我在get-request回调函数中添加了console.log(getClientIp())。感谢您的重播,指出了我的错误。
console.log(getClientIp(););