0

I have a Application Built on Node.js & Express with Mongodb(MEN).its working but i wanted to store the IP's from where my App is getting accessed more and for some other Info.i used diff npm modules But unable to get Client IP Address.instead of Client Address its giving public IP of Server where it is running on. My app Structure Looks like Below..

FIRST APP

My_APP_FOLDER|--> app------> views,models,controllers
             |--> public---> js,css 
             |--> config---> routes.js
             |server.js
             |package.json

Express is intiated in server.js file var app = express(); and exported module.exports = app; and i'm loading all routes.js , public files using require() in sever.js file.

so whenever request comes it will hit routes.js. it will check for matching /urlpath and will execute the controller function. which is mentioned something like this app.get('/urlpath',contoller.functionname);.

controller function will look like this

exports.index = function (req, res) { 
//logic mentioned here
console.log(req.ip);//here im seeing public IP address of server where i hosted app  instead client of IP address from where request has come.
});

and i tried same with other application. which is single file without multiple folders n files.

SECOND APP

var express = require('express');
 var app = express();
 app.get('/', function (req, res) {
     // console.log(req.ip); and tried with some other modules too getting            IP address of Client which is exactly what i need
 });

So why i'm unable to get IP of Client in First App and why i'm getting in Second.any suggestions to get IP in First App.Thanks in Advance

Soviut
  • 79,529
  • 41
  • 166
  • 227
sandeep
  • 89
  • 9
  • Next time, please insert your code blocks properly, don't use backticks. There's button built right into the markdown editor. I fixed it for you this time. – Soviut Oct 23 '16 at 17:40
  • for Sure @Soviut. – sandeep Oct 23 '16 at 17:42
  • please read the question properly @jfriend00 i'm able to get the IP address but not the client's (from where the request has come. it's showing the IP address of Server where the APP is hosted). – sandeep Oct 24 '16 at 03:44
  • I read it just fine. That's exactly what the dup is about. It's about getting the remote client IP address. If it doesn't work for you, then there's something odd about your environment. Does your hosting use a proxy? If so, your server may not even see the actual remote address or you have to get it from a header. – jfriend00 Oct 24 '16 at 03:51
  • thank you for spending your time @jfriend00 . i have no idea about proxy. and one more thing is --> Does FirstApp Folder structure causes any problem ? – sandeep Oct 24 '16 at 03:55
  • Folder structure has nothing to do with IP address retrieval. – jfriend00 Oct 24 '16 at 04:03
  • our Application Main page (home page) will be maintained by NGINX is that reason ? @jfriend00 – sandeep Oct 24 '16 at 07:05
  • Probably. nginx is a proxy so your server actually gets a connection from nginx, not from the client. If you read the dup, it talks specifically about nginx and tells you to look in the headers for the original client IP address if nginx is properly configured. – jfriend00 Oct 24 '16 at 07:06
  • got it by configuring NGINX... Thanks for the suggestions @jfriend00 – sandeep Oct 26 '16 at 06:05

0 Answers0