我只想检查客户端是否为localhost
。如果没有,我想将所有静态文件请求都出错。但这并不像我想象的那么容易。
我尝试了here,here,here,here的所有解决方案,但是没有运气。
我的后端在localhost:11630上运行,前端在staging.***.com
上运行
app.set('trust proxy', true);
app.use(function (req, res, next) {
console.log('req.get("origin"): ', req.get('origin'));
console.log('req.headers.host: ', req.headers.host);
console.log('req.headers.hostname: ', req.headers.hostname);
console.log('req.headers.origin: ', req.headers.origin);
console.log('req.headers.origin: ', req.headers.origin);
console.log('req.hostname: ', req.hostname);
console.log('req.ip: ', req.ip);
console.log('req.ips: ', req.ips);
console.log('req.headers["x-forwarded-for"] || req.connection.remoteAddress: ', req.headers['x-forwarded-for'] || req.connection.remoteAddress);
console.log('req.ipInfo: ', req.ipInfo);
next();
})
结果:
req.get("origin"): undefined
req.headers.host: localhost:11630
req.headers.hostname: undefined
req.headers.origin: undefined
req.headers.origin: undefined
req.hostname: localhost
req.ip: ::1
req.ips: []
req.headers["x-forwarded-for"] || req.connection.remoteAddress: ::1
req.ipInfo: undefined
没有 app.set('trust proxy',true); 的结果:
req.get("origin"): undefined
req.headers.host: localhost:11630
req.headers.hostname: undefined
req.headers.origin: undefined
req.headers.origin: undefined
req.hostname: localhost
req.ip: ::1
req.ips: []
req.headers["x-forwarded-for"] || req.connection.remoteAddress: ::1
req.ipInfo: undefined
有人可以提出建议吗?