admin管理员组文章数量:1023187
I am learning Node.js. While creating a web site, I will run the web site locally (on localhost
). When it is deployed, it will run on other servers. My question is, how do I determine if a request is from localhost or not in Node? In ASP.NET, I could use Request.IsLocal
. I'm trying to figure out how to do that in Node.
Thank you!
I am learning Node.js. While creating a web site, I will run the web site locally (on localhost
). When it is deployed, it will run on other servers. My question is, how do I determine if a request is from localhost or not in Node? In ASP.NET, I could use Request.IsLocal
. I'm trying to figure out how to do that in Node.
Thank you!
Share Improve this question asked Jan 25, 2015 at 13:04 JQuery MobileJQuery Mobile 6,30124 gold badges88 silver badges138 bronze badges1 Answer
Reset to default 4There's server.address()
to get the server address.
And request
has connection
and socket
objects, as both might hold remote address (in a remoteAddress
property) depending on a type of current connection.
But if the server is behind a reverse proxy, you'll have to pull it from appropriate header, most likely x-forwarded-for
. However I'm not sure if that holds if proxies are chained.
So, to conclude, you'd do something along the lines of:
function cliAddress(req) {
return req.connection.remoteAddress || req.socket.remoteAddress || req.headers['x-forwarded-for'];
}
server.isLocal = function(request) {
return server.address() === cliAddress(req);
}
And if you use express.js see Express.js Req.IP API.
I am learning Node.js. While creating a web site, I will run the web site locally (on localhost
). When it is deployed, it will run on other servers. My question is, how do I determine if a request is from localhost or not in Node? In ASP.NET, I could use Request.IsLocal
. I'm trying to figure out how to do that in Node.
Thank you!
I am learning Node.js. While creating a web site, I will run the web site locally (on localhost
). When it is deployed, it will run on other servers. My question is, how do I determine if a request is from localhost or not in Node? In ASP.NET, I could use Request.IsLocal
. I'm trying to figure out how to do that in Node.
Thank you!
Share Improve this question asked Jan 25, 2015 at 13:04 JQuery MobileJQuery Mobile 6,30124 gold badges88 silver badges138 bronze badges1 Answer
Reset to default 4There's server.address()
to get the server address.
And request
has connection
and socket
objects, as both might hold remote address (in a remoteAddress
property) depending on a type of current connection.
But if the server is behind a reverse proxy, you'll have to pull it from appropriate header, most likely x-forwarded-for
. However I'm not sure if that holds if proxies are chained.
So, to conclude, you'd do something along the lines of:
function cliAddress(req) {
return req.connection.remoteAddress || req.socket.remoteAddress || req.headers['x-forwarded-for'];
}
server.isLocal = function(request) {
return server.address() === cliAddress(req);
}
And if you use express.js see Express.js Req.IP API.
本文标签: javascriptDetermine if Request is Local in Nodejs appStack Overflow
版权声明:本文标题:javascript - Determine if Request is Local in Node.js app - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745565564a2156426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论