node.js - check real-ip IP address with javascript for nodejs -


i have code part of nodejs application runs behind nginx proxy:

var ip_address = (request.headers['x-real-ip']); if (ip_address !== "127.0.0.1") { var ip = request.headers['x-real-ip']; console.log(ip); } else { var ip = "173.194.41.100"; console.log(ip); } 

what trying achieve on local dev machine, testing use fixed ip address, otherwise reads x-real-ip request header.

my question, there way make more full proof, example, there way pass loop making var ip return null, create traceback on app?

my question, there way make more full proof, example, there way pass loop making var ip return null, create traceback on app?

if change slightly, can sure ip not null:

var ip, ip_address; ip = ip_address = request.headers['x-real-ip']; if (ip === null || ip === "127.0.0.1") { ip = "173.194.41.100"; } console.log(ip); 

your original code make ip null if request.headers['x-real-ip'] evaluated null, because condition (ip_address !== "127.0.0.1") true, , you'd grab request.headers['x-real-ip'] (null) ip.

with modified code above, don't have problem, , we've been able make bit more concise well.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -