Verify nodejs connection comes from localhost

Is there a way to check where the nodejs connection comes from?

in javascript we do

if (window.location.host == "localhost")
{
    // Do whatever
}

however I have no idea how to do this in nodejs, I want to do (then I will only need to save 1 folder for git repo)

if (window.location.host == "localhost"){
    // connect to localhost mongodb
}else{
    // connect to mongodb uri
}
+4
source share
3 answers

Check it out req.headers.host. If it is localhost or 127.0.0.1, use your logic to create localhost files. http://nodejs.org/api/http.html#http_http_incomingmessage

req.headers.host also contains a port that you may need to secure before checking.

+2
source
var os = require('os');
var database_uri;

if(os.hostname().indexOf("local") > -1)
  database_uri = "mongodb://localhost/database";
else
  database_uri = "mongodb://remotehost/database";

//Connect to database
mongoose.connect(database_uri, 
                 function(err){
                   if(err) console.log(err); 
                   else console.log('success');});
+2
source

:

req.connection.remoteAddress

+1

All Articles