Reputation: 895
So basically the title says it all.
For the past few hours I have been working on a middleware function that relies on cookies for authentication, something very simple like this:
const authRoute = async (req, res, next) => {
console.log(req.ip);
// some other logic here
return next();
};
app.use('/admin/', authRoute, adminRouter);
The thing is, I haven't modified any of my other code such as for example app.set()
or even the app.use()
itself, I have been working on this middleware function exclusively. I am also storing this req.ip
in my database for auth purposes.
Well, to my surprise this very same req.ip
object all of a sudden started to result in:
::1
just after an application/server restart for seemingly no reason. Right before that it was reporting back to me:
::ffff:127.0.0.1
I can verify that because I am console logging this and storing the value in a database as well.
For the record, req.connection.remoteAddress
is the same, and I don't have any special settings such as trust proxy
or anything like that enabled, I haven't reached that stage of development yet.
So I am really scratching my head right now of what might be causing this, so any help or input is appreciated. I can provide further details as well such as my bin/www
config if necessary.
I am on a Win7 machine.
Upvotes: 1
Views: 637
Reputation: 30837
Check the string for leading ::ffff:
and remove it then the remaining is IPv4, otherwise (without leading ::ffff:
) it is an IPv6.
The reason you are getting different IPs for localhost
is sometimes the request is coming as IPv4 (::ffff:127.0.0.1
) and sometimes as IPv6 (::1
).
Anyway you need to take care of both versions in production.
Upvotes: 1
Reputation: 177
You can try to use req.connection.remoteAddress
, i have used it many times with success .
This answer is crystal clear https://superuser.com/questions/668004/why-is-my-localhost-not-127-0-0-1-but-1-and-what-notation-is-that
Upvotes: 1