Express.js: How can I get the ip Address and render a view?

非 Y 不嫁゛ 提交于 2019-12-06 04:56:56

问题


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:


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.



来源:https://stackoverflow.com/questions/25192216/express-js-how-can-i-get-the-ip-address-and-render-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!