Open johnboxall opened 7 months ago
In the context of ssr.js
, if you need to pull the client IP out of you'll need to grab it from the HTTP request header x-forwarded-for
when the app is deployed to MRT:
// ssr.js
app.get('/ip', function handleIp(req, res) {
const ip = (req.get('x-forwarded-for') || '').split(',')[0] || req.ip
return res.json({ip})
})
If you need the IP in SCAPI, you can send it using a Custom Query Parameter or Custom Header:
In Express.js, you can you the Request object's properties
req.ip
andreq.ips
to access information about the IP address used to make a request:https://expressjs.com/en/api.html#req.ip https://expressjs.com/en/api.html#req.ips
During local development, these properties return the correct IP, but when deployed to MRT, they no longer work as the true client IP is now contained in the
x-forwarded-for
header.You can convince Express to trust the
x-forwarded-for
header and use its value by enabling the trust proxy setting:When we detect that the app is running in MRT, we should set this setting!
https://expressjs.com/en/4x/api.html#app.settings.table