Express.js: How can I get the ip Address and rende

2020-07-18 06:47发布

I really think this should be easy. But when I render a jade template, I also want to grab the ip address. My code look like this.

app.js

app.get('/', index.home)

index.js

exports.home = function(req, res) {
    res.render('index');
};

Where can I add something like:

var ip = req.header('x-forwarded-for') || req.connection.remoteAddress; //or
console.log(req.connection.remoteAddress);

1条回答
Explosion°爆炸
2楼-- · 2020-07-18 07:17

Just use req.ip and make sure you have app.enable('trust proxy'); if your app is deployed behind a reverse proxy. Express has all the header parsing and proxy logic baked in for you.

查看更多
登录 后发表回答