问题
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