check is nodejs connection come from localhost

javascript, node.js

Solution

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');});

Problem

Is there a way to check where the nodejs connection come from? in javascript we do ``` if (window.location.host == "localhost") { // Do whatever } ``` however I have no idea how to do it in nodejs, I want to do ( then I'll only need to maintain 1 folder for the git repo ) ``` if (window.location.host == "localhost"){ // connect to localhost mongodb }else{ // connect to mongodb uri } ```

Original source

Related problems