angularjs - 使用Nodejs、Express和AngularJS在浏览器中显示IP

标签 angularjs node.js express ip-address

我正在学习 Nodejs 和 ExpressJS。我正在尝试使用 ExpressJS 和 2 个 Node 模块(request-ipgeoip2)来获取用于地理定位的客户端 IP 地址,然后使用 AngularJS (1.x) 在浏览器中输出地理定位。

到目前为止,我的 Nodejs 和 Expressjs 代码是

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

对于 Angular,我有

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);
        })
      }; 
    };
Angular Controller 中的

vm.result 用于执行地理定位的 geoip2 Node 模块的结果。

我可以在控制台中获取结果,没问题,但我不确定如何将它传递给 Angular。我正在使用 $http 服务,但我不确定从这里到哪里...?

如何使用 $http 将 result 从 geoip2 Node 模块传递到我的 Angular Controller ?

最佳答案

问题是你在完成之前调用 next。

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

然后在 Angular 上

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

如果要使用用户IP获取位置:

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

关于angularjs - 使用Nodejs、Express和AngularJS在浏览器中显示IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41944036/

相关文章:

javascript - 带有 ajax 和 Node http 模块的 Node.js 管道流

javascript - 当从 AngularJS 中的 API 响应收到错误时,触发错误弹出模式的最佳方法是什么?

angularjs - 圆圈内的样式文本

javascript - 使用 Passport.js 的登录系统始终执行 “failureRedirect” (nodejs)

javascript - Express服务器响应是一个流,如何使其成为字符串/json

javascript - 将 Flash 消息与 Angular Routing 结合使用

javascript - Angular - 路由为什么我的 Controller 被调用两次

angularjs - 如何使用 AngularJS 过滤数组并使用过滤对象的属性作为 ng-model 属性?

javascript - 在类内部使用箭头函数并在构造函数中使用它 - nodejs

javascript - 在 Node 中高效地逐行读取文件