Carriemf
Carriemf

Reputation: 169

Finding client ip express

I'm building a basic web survey and want to get the client's ip address to track them through the website. I've looked through some other stackoverflow questions and followed their advice but am getting a strange output. From the following code:

index.js:

/*POST users*/
router.post('/users', function(req, res, next) {
    req.body.ip = req.connection.remoteAddress;
    console.log(req.body);
    var user = new User(req.body);

    user.save(function(err,user) {
        if(err) {
            return next(err);
        }

        res.json(user);
    });
});

I'm getting a mongo document like this:

"_id" : ObjectId("55cb531714b922ad2478ae9f"),
    "ip" : "::1",
    "responses" : [
        {
            "questionId" : "55c9fbb0401a5ba71ce5181a",
            "optionValue" : 2,
            "_id" : ObjectId("55cb531714b922ad2478aea1")
        },
        {
            "questionId" : "55c9fbe90f6fefa91cb4fc2d",
            "optionValue" : 2,
            "_id" : ObjectId("55cb531714b922ad2478aea0")
        }
    ],
    "__v" : 0

Any ideas why I might be getting this output?

Upvotes: 1

Views: 63

Answers (1)

Cristian Lupascu
Cristian Lupascu

Reputation: 40576

That's because ::1 is the IPv6 shorthand for localhost.

Your program seems to work fine; try accessing the site from a remote computer to get a normal-looking IP.

Upvotes: 1

Related Questions