Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have create a nodejs server which will give response as ip address

app.get('/UserIP', function(req, res) {
    console.log(req.connection.remoteAddress);
  res.send(JSON.stringify({'ip':req.connection.remoteAddress}));
});

later i created a client which will get this ip address

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
 $(document).ready(function () {

    $.getJSON("http://localhost:7979/userip", function (data) {
                $('p').html('IP Address is: ' + res.ip);
    });
});
</script>
</head>
<body>
<p></p>
</body>
</html>

I cant get that ip to my client website.there are websites like jsonip.com which are working with my code. please help me in understanding where i m doing mistake.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
147 views
Welcome To Ask or Share your Answers For Others

1 Answer

  • If you have app.set('case sensitive routing', true); in your app , accessing "/userip" instead of "/UserIP" will not work. You have app.get('/UserIP' and $.getJSON("http://localhost:7979/userip".
  • In your jQuery code , you are accessing a "res" Object + res.ip) , even though you are returning a "data" function (data) Object.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...