Using Nodejs, Express and AngularJS to display IP

2019-05-13 16:23发布

问题:

I'm learning Nodejs and ExpressJS. I'm trying to use ExpressJS and 2 Node modules (request-ip and geoip2) to get the client IP address for geolocation and then outputting the geolocation in the browser using AngularJS (1.x).

So far for my Nodejs and Expressjs code I have

    var express = require('express');
// require request-ip and register it as middleware
var requestIp = require('request-ip');
// to convert the ip into geolocation coords
var geoip2 = require('geoip2');

// Init app
var app = express();
var port = process.env.PORT || 8000;

geoip2.init(); // init the db

//app.use(requestIp.mw({ attributeName: 'myCustomAttributeName'}));
var ip = '207.97.227.239';//67.183.57.64, 207.97.227.239

// respond to homepage req
app.get('/', function (req, res, next) {
    //var ip = req.myCustomAttributeName;// use this for live
    //var ip = '207.97.227.239';/* use this for testing */
    console.log('requestIP is ' + ip);
    next();
    // geolocation
    geoip2.lookupSimple(ip, function(error, result) {
      if (error) {
        console.log("Error: %s", error);
      }
      else if (result) {
        console.log(result);//ipType was causing console.log duplication, IDK why
      }
    });
});

// set static folder
app.use('/', express.static(__dirname + '/public'));

app.listen(port, function(){
    console.log('user location app is running');
});

And for Angular I have

angular.module('UserLocation', []);

angular.module('UserLocation')
    .controller('MainController', MainController);

MainController.$inject = ['$http'];

function MainController($http) {
    var vm = this;
    vm.result = '';

    vm.message = 'Hello World';
    vm.getLocation = function() {
        console.log();
        return $http.get('localhost:8000', {
        params: {result: result}
      })
      .then(function(result){
        console.log(result);
        })
      }; 
    };

vm.result in the Angular controller is for the result from the geoip2 Node module that performs the geolocation.

I can get the result in the console no problem but I'm not to sure how to pass it to Angular. I'm using the $http service but I'm not sure where to go from here...?

How do I pass the result from the geoip2 Node module to my Angular controller with $http?

回答1:

The problem is that you are calling next before you are even done.

app.get('/', function (req, res, next) {
    //next(); this line should be commented
    // geolocation
    geoip2.lookupSimple(ip, function(error, result) {
      if (error) 
        return res.status(400).json({error: 'Something happened'});

      return res.send(result);
    });
});

Then on angular

$http({
  method: 'GET',
  url: '/yourURL'
}).then(function (response) {
  console.log(response);
});

If you want to use the user IP to get location:

app.get('/', function (req, res, next) {
    //next(); this line should be commented
    // geolocation

    var ip = req.headers['x-forwarded-for'] || 
     req.connection.remoteAddress || 
     req.socket.remoteAddress ||
     req.connection.socket.remoteAddress;

    geoip2.lookupSimple(ip, function(error, result) {
      if (error) 
        return res.status(400).json({error: 'Something happened'});

      return res.send(result);
    });
});