How do I know if a request is being made from a local machine using Node and / or Express?
2 answers
The following code should work with IPv4 and IPv6 (untested). It uses the same validation that Microsoft's System.Web.HttpRequest.IsLocal uses - check that the request is from 127.0.0.1
or from the server IP . This matches IPv6 and if the server is bound to a specific IP address.
if (req.ip === '127.0.0.1' || req.ip === server.address.address) {
// do something
} else {
// do something else
}
0
source to share