如何让ip客户端使用express 4.x [英] how get ip client using express 4.x

查看:163
本文介绍了如何让ip客户端使用express 4.x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要获得客户端的IP,我正在尝试使用localhost (127.0.0.1),但是我总是得到 :: 1

I want to get the client's IP and I 'm trying with localhost (127.0.0.1 ) but I always get :: 1

我正在尝试使用

app.enable('trust proxy');
app.set('trust proxy', 'loopback');

app.get('/',function(req,res){
 res.send(req.ip); //I always get :: 1
 // or
 var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    res.send(ip);//I always get :: 1
});

如何获得 127.0.0.1 :: 1 。这可以做吗?

how can get 127.0.0.1 and not :: 1 . this can be done?

推荐答案

:: 1 是IPv6等价物的 localhost 。如果您只想让您的服务器通过IPv4进行侦听,因此只有客户端有IPv4地址,则可以在 app.listen()中指定IPv4地址: / p>

::1 is the IPv6 equivalent of localhost. If you want to only have your server listen over IPv4 and thus only have IPv4 addresses come in from your clients, you can specify an IPv4 address in app.listen():

app.listen(3000, '127.0.0.1');

这篇关于如何让ip客户端使用express 4.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆