回覆列表
-
1 # tkosh25997
-
2 # 使用者8852944140293
因為涉及相容性問題,可以透過req.header("x-forwarded-for")與req.connection.remoteAddress來獲取IP地址,下面是封裝好的一個函式:
function getClientIp(req) {
var ipAddress;
var forwardedIpsStr = req.header("x-forwarded-for");
if (forwardedIpsStr) {
var forwardedIps = forwardedIpsStr.split(",");
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
因為涉及相容性問題,可以透過req.header("x-forwarded-for")與req.connection.remoteAddress來獲取IP地址,下面是封裝好的一個函式: