使用Nodejs,Express和AngularJS在浏览器中显示IP [英] Using Nodejs, Express and AngularJS to display IP in the browser
问题描述
我正在学习Nodejs和ExpressJS.我正在尝试使用ExpressJS和2节点模块( request-ip 和 geoip2 )以获取用于地理位置的客户端IP地址,然后在浏览器中使用AngularJS(1 .x).
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).
到目前为止,我拥有的Nodejs和Expressjs代码
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');
});
对于Angular我有
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);
})
};
};
Angular控制器中的
vm.result
用于执行地理定位的geoip2节点模块的结果.
vm.result
in the Angular controller is for the result from the geoip2 Node module that performs the geolocation.
我可以在控制台中获取result
没问题,但是我不确定如何将其传递给Angular.我正在使用$http
服务,但是我不确定从这里去哪里...?
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...?
如何通过$ http将result
从geoip2节点模块传递到我的Angular控制器?
How do I pass the result
from the geoip2 Node module to my Angular controller with $http?
推荐答案
问题是您甚至在完成操作之前就先打电话.
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);
});
});
然后按角度
$http({
method: 'GET',
url: '/yourURL'
}).then(function (response) {
console.log(response);
});
如果要使用用户IP获取位置:
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);
});
});
这篇关于使用Nodejs,Express和AngularJS在浏览器中显示IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!