IP在Node.jsexpress中报道为null ...如何可能?
我有一个带有Node.js/Express后端的网站,我需要记录访问者的IP地址。看来这应该是一个相当简单的过程,但是我对此持续困难。我的代码相似:
const express = require('express');
const app = express();
var port = 443;
app.get('/', function (req, res) {
var site = req.hostname; //example returns "localhost" from "localhost:8080"
var splits = site.split("."); //"split" on "periods"
var subdomain = String(splits[1])
var _host = req.headers.host;
var _userIP = req.headers['x-forwarded-for'] || req.socket.remoteAddress || null;
console.log('_userIP is: ', _userIP); //returns 'null'
//...
});
//...
// Start server...
var webServer = app.listen(process.env.PORT || port, function(request, response) { console.log(" Received request for " + new Date()); } );
console.log('Listening on port ' + port);
//...
console.log("***CREATING WEBSOCKET SERVER");
var wsServer = new WebSocketServer({
httpServer: webServer,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});
在指导中,我使用以下源以获取信息:“ https://stackoverflow.com/questions/8107856/how-to-to-determine-a-a-users-users-ip-address-indress-inode”。
我的console.log语句返回“ null”作为'_userip'变量的值...为什么???据我了解,与服务器建立的任何连接都应该具有某种IP地址,正确吗?如果是这种情况,那么“ null”是我的“后备”变量定义,无论是我的“后备”变量定义,但是……
任何建议都非常感谢。
顺便说一句,我还尝试将“ _userip”变量分配给“ req.ip”为:
console.log('_userIP is: ', req.ip);
...没有成功。我相信如果我没记错的话,它返回了“未定义”。
我还尝试了:
_userIP = req.header('x-forwarded-for') ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
此代码引发错误,我相信,因为'req.connection.socket.remoteaddress'已弃用...?
如果它有所不同,我相信该代码显示“ :: 1”,如果它是在localhost上运行的……这表明它是功能性的。在“真实”服务器上执行时,为什么代码无法显示用户IP地址?我的服务器软件(Plesk)是否有可能阻止IP地址?
I have a website with a Node.js/Express backend and I need to log the visitor's IP address. It seems this should be a fairly straightforward process, however I am having continuous difficulty with it. My code is similar:
const express = require('express');
const app = express();
var port = 443;
app.get('/', function (req, res) {
var site = req.hostname; //example returns "localhost" from "localhost:8080"
var splits = site.split("."); //"split" on "periods"
var subdomain = String(splits[1])
var _host = req.headers.host;
var _userIP = req.headers['x-forwarded-for'] || req.socket.remoteAddress || null;
console.log('_userIP is: ', _userIP); //returns 'null'
//...
});
//...
// Start server...
var webServer = app.listen(process.env.PORT || port, function(request, response) { console.log(" Received request for " + new Date()); } );
console.log('Listening on port ' + port);
//...
console.log("***CREATING WEBSOCKET SERVER");
var wsServer = new WebSocketServer({
httpServer: webServer,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});
For guidance I am using the following source for information: "https://stackoverflow.com/questions/8107856/how-to-determine-a-users-ip-address-in-node".
My console.log statement returns "null" as the value for the '_userIP' variable...why??? As I understand ANY connection made to the server should have some sort of IP address, correct? If that is the case then the 'null' as my 'fallback' variable definition should never be assigned, however it is...
Any advice is greatly appreciated.
BTW I have also attempted assigning the '_userIP' variable to "req.ip" as:
console.log('_userIP is: ', req.ip);
...without success. I believe it returned 'undefined' if I remember correctly.
I have also tried:
_userIP = req.header('x-forwarded-for') ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
This code throws an error, I believe because the 'req.connection.socket.remoteAddress' is deprecated...?
If it makes a difference I believe this code displays "::1" if it is run on localhost...which indicates it is functional. Why would the code fail to display a user IP address when executed on a 'real' server? Is it possible my server software (Plesk) may be somehow blocking the IP address?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论