4 thoughts on “Identify if request is local or remote.

  1. The easier way to is to set an environmental variable for what environment you’re in(production or development). The usual one is process.env.NODE_ENV then do a check like

    if (process.env.NODE_ENV === 'development') {
    // do dev stuff
    if (process.env.NODE_ENV === 'production') {
    //do production stuff

    You also have to run the server like this NODE_ENV="development" node server.js. The other option is to check the ip address https://stackoverflow.com/questions/10849687/express-js-how-to-get-remote-client-address and filter for local addresses ( but that could be inconsistent (it might not register as

  2. The absolute best way is as @toastynerd describes by simply using configuration to configure your app differently based on it’s environment. Doing anything dynamically can lead to security issues in the long run. Otherwise, as long as you don’t have a proxy on the same machine, checking req.connection.remoteAddress for or ::ffff: or ::1 will let you know if the raw socket is from your localhost (the raw socket address cannot be spoofed).

  3. Therefore I use this

    var isThisLocalhost = function (req){
        var ip = req.connection.remoteAddress;
        var host = req.get('host');
        return ip === "" || ip === "::ffff:" || ip === "::1" || host.indexOf("localhost") !== -1;
  4. For simple cases, this would be enough:

    var isLocal = (req.connection.localAddress === req.connection.remoteAddress);

Comments are closed.