// Module dependencies
var express = require('express'),
    reverse = require('dns').reverse;

// Middleware to extract ipInfo from the request
function ipInfo(req, res, next) {
  // Check for an `X-Forwarded-For` header, so the server can be run behind an
  // nginx reverse proxy, then fallback to `req.connection.remoteAddress`
  var ip = req.header('x-forwarded-for') || req.connection.remoteAddress;

  // Add the ip address to `req.ipInfo`
  req.ipInfo = {ip: ip};

  // Check the querystring for a `domains` parameter, so when the user makes
  // a request to `/?domains=1` it will resolve the domain name(s).
  if (req.query.domains) {
    // Perform a reverse dns lookup on the ip address
    reverse(ip, function(err, domains) {
      if (err) next(err);

      // Store the returned domains and continue down the stack
      req.ipInfo.domains = domains;
      next();
    });
  } else {
    // Carry on down the stack
    next();
  }
}

function linkHeader(link, rel) {
  return function(req, res, next) {
    if (link.charAt(0) === '/') {
      console.log(req.headers.host)
      link = "http://" + req.headers.host + link;
    }
    var previous = res.header('Link');
    if (previous) {
      previous += ', ';
    } else {
      previous = '';
    }
    res.header('Link', previous + '<' + link + '>; rel="' + rel + '"');
    next();
  };
}

// Create a simple middleware stack
var app = module.exports = express.createServer(
  ipInfo,
  express.logger(),
  linkHeader('https://github.com/hecticjeff/ipserver', 'help')
);

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

app.enable('jsonp callback');

var jsonLink = linkHeader('/json', 'alternate');

app.get('/', jsonLink, function(req, res) {
  res.send(req.ipInfo.ip, {'Content-Type': 'text/plain'});
});

var plainLink = linkHeader('/', 'alternate');

app.get('/json', plainLink, function(req, res) {
  res.send(req.ipInfo);
});


if (!module.parent) {
  app.listen(3000);
  console.log("jsonp server running on port %s", app.address().port);
}

@@ -176,236 +176,8 @@
 ) %7B%0A
-  // Check for an %60X-Forwarded-For%60 header, so the server can be run behind an%0A  // nginx reverse proxy, then fallback to %60req.connection.remoteAddress%60%0A  var ip = req.header('x-forwarded-for') %7C%7C req.connection.remoteAddress;%0A%0A
   //
@@ -232,16 +232,20 @@
  = %7Bip: 
+req.
 ip%7D;%0A%0A  
@@ -480,16 +480,20 @@
 reverse(
+req.
 ip, func
@@ -830,44 +830,8 @@
 ) %7B%0A
-      console.log(req.headers.host)%0A
     
@@ -901,30 +901,27 @@
 vious = res.
-header
+get
 ('Link');%0A  
@@ -1010,22 +1010,19 @@
     res.
-header
+set
 ('Link',
@@ -1165,35 +1165,36 @@
 ress
-.createServer(%0A  ipInfo,%0A  
+()%0Aapp.use(ipInfo);%0Aapp.use(
 expr
@@ -1205,20 +1205,27 @@
 logger()
-,%0A  
+);%0Aapp.use(
 linkHead
@@ -1276,17 +1276,16 @@
  'help')
-%0A
 );%0A%0Aapp.
@@ -1478,24 +1478,50 @@
 er());%0A%7D);%0A%0A
+app.enable('trust proxy')%0A
 app.enable('
@@ -1645,27 +1645,10 @@
 s.se
-nd(req.ipInfo.ip, %7B
+t(
 'Con
@@ -1657,17 +1657,17 @@
 nt-Type'
-:
+,
  'text/p
@@ -1671,17 +1671,43 @@
 t/plain'
-%7D
+);%0A  res.send(req.ipInfo.ip
 );%0A%7D);%0A%0A
@@ -1878,76 +1878,6 @@
 0);%0A
-  console.log(%22jsonp server running on port %25s%22, app.address().port);%0A
 %7D%0A

Update to work with latest version of express