我正在使用express.js,需要知道发起调用的域名。以下是简单的代码:
app.get(
'/verify_license_key.json',
function( req, res ) {
// do something
我怎样从req
或res
对象中获取域名?
我的意思是我需要知道API是由somesite.example
还是someothersite.example
调用的。
我尝试通过对req
和res
进行console.dir,但是我无法从其中得到任何想法,同时也阅读了文档,但没有帮助。
req.host
或req.get('host')
来获取主机名 参考文档。 - dc5req.headers["x-forwarded-for"] || req.connection.remoteAddress
x-forwarded-for 可以在代理、负载均衡等情况下覆盖您的基础需求。 - Eat at Joes