「Node.js」アクセス元のIPアドレスを取得する
書式
アクセス元のIPアドレス=request.headers['x-forwarded-for’];
サンプルコード
var clientIP = function(request){ var ipAddr = '172.17.0.0'; if (request.headers['x-forwarded-for']) { ipAddr = request.headers['x-forwarded-for']; }else if (request.connection && request.connection.remoteAddress) { ipAddr = request.connection.remoteAddress; }else if (request.connection.socket && request.connection.socket.remoteAddress) { ipAddr = request.connection.socket.remoteAddress; }else if (request.socket && request.socket.remoteAddress) { ipAddr = request.socket.remoteAddress; } return ipAddr; };
説明
request.headers['x-forwarded-for’]でアクセス元のIPアドレスを取得します