2017-02-04 5 views
1

NodeJS 및 Express를 익히고 있습니다. 나는 2 개의 노드 모듈과 함께 노드와 익스프레스를 사용하여 클라이언트의 IP 주소를 얻는다. 그 부분은 잘 작동합니다. AngularJS (1.x)를 사용하여 Angular를 사용하여 브라우저의 노드 모듈에서 Geolocation 데이터를 표시하는 데 문제가 있습니다. 브라우저에서 Angular를 통해 표시 할 지리적 데이터를 가져올 수 없기 때문에 잘못된 결과를 나타냅니다.ExpressJS 및 AngularJS 사용

// get IP address 
app.get('/ip', 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); 
    // geolocation 
    geoip2.lookupSimple(ip, function(error, result) { 
    if (error) { 
     return res.status(400).json({error: 'Something happened'}); 
     } 
     else if (result) { 
     return res.send(result); 
     } 
    }); 
}); 

는 내가 익스프레스가 제대로

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

그리고 AngularJS와를 제공 내/공용 폴더의 index.html을 내 각도 응용 프로그램

<html ng-app="UserLocation"> 
<title>The MEAN Stack</title> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script> 
</head> 
<body ng-controller="MainController as vm"> 
    <h1 style="color:blue;">{{ vm.message }}</h1> 
    <br> 
    <h2 style="color:red;">{{ vm.location }}</h2> 
    <script src="/assets/js/app.js"></script> 
</body> 
</html> 

그리고 내 정적 파일을 제공 믿습니다 각도 앱

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

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

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

/*function ctrlFunc() { 
    this.message = 'Hello World again'; 

};*/ 

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

    vm.message = 'Hello World'; 
    // user location 
    //function getLocation() { 
    vm.getLocation = function() { 
     $http({ 
      method: 'GET', 
      url: 'localhost:8000/ip' 
     }).then(function (result) { 
      console.log(result); 
      return vm.location = result; 
     }); 
     }; 
    }; 

는 IP 작업 및 '/ip'에 표시되지만 '/'에서 나는 지리 데이터에 잘못 뭐하는 거지 Cannot GET / 가 '/'로 표시 할 수

+0

그 경로를 정의 했습니까? 뭔가 :'app.get ('/', function() {...})' –

+0

@NehalJWani 아, 고마워! 나는 그런 것들을 놓치고 있다는 의혹을 가지고있었습니다 ... 확실하지 않았습니다 ... 여전히 Express를 배우십시오. – user3125823

답변

1

클라이언트 측 응용 프로그램이 처리 할 수 ​​있도록 라우팅

// All other routes should redirect to the index.html 
app.route('/*') 
    .get((req, res) => { 
    res.sendFile(path.resolve(__dirname + '/public/index.html')); 
});