从Express获取Google Maps Geocoding JSON [英] Get Google Maps Geocoding JSON from Express

查看:56
本文介绍了从Express获取Google Maps Geocoding JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在自学Node.js和Express,并且尝试从Google Maps Geocoding API请求返回JSON结果.我已经使用require模块使它正常工作,但是我想弄清楚我在Express中做错了什么,所以我了解了这一点:

I've been teaching myself Node.js and Express, and I am trying to return the JSON result from a Google Maps Geocoding API request. I have gotten it to to work using the require module, BUT I am trying to figure out what I did wrong in Express so I have learned it:

快速尝试:htmlController.js

// FYI: This controller gets called from an app.js file where express() and 
// the mapsAPI is passed as arguments.

var urlencodedParser = bodyParser.urlencoded({extended: false});

module.exports = function(app, mapsAPI){
app.post('/maps', urlencodedParser, function(req,results){
   var lat;
   var long;
   var add = req.body.add;
     app.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + add + '&key=' + mapsAPI, function(req,res){
        lat = res.results.geometry.northeast.lat;
        long = res.results.geometry.northeast.long;
        console.log(lat); // no output
        console.log(lat); // no output
  }, function(){
        console.log(lat); // no output
        console.log(long); // no output
  });
  results.send("Thanks!");
});
}

如您所见,我试图将其记录在不同的代码块中,但是API请求中的任何日志都不会显示在控制台上.

As you can see, I am trying to log it in different code blocks, but any log inside the API request is not getting shown to the console.

使用require模块的工作请求:

Working Request using the require Module:

app.post('/maps', urlencodedParser, function(req,results){
  var add = req.body.add;
  request({
  url: 'https://maps.googleapis.com/maps/api/geocode/json?address=' + add + '&key=' + mapsAPI,
  json: true
}, function (error, response, body) {
    if (!error && response.statusCode === 200) {
      console.log(body) // Print the json response
    }
  });
  results.send("Thanks!");
});

推荐答案

如果我对您的理解正确,则您正在尝试使用app.get()

If I understand you correctly, you are trying to get data from maps api by using app.get()

app.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + add + '&key=' + mapsAPI, function(req,res){}

但是app.get()函数仅用于您的应用程序路由,而不用于获取远程数据.与router.get()

But app.get() function is used for your app route only, not to fetch remote data. same for router.get()

// app.get(appRoute, middlewareORhandler)
app.get('/myOwnApp/api/users/12/', function(res,res,next)){
    // your code here
    res.status(200).send("return your response here");
}

要发出远程请求,可以使用内置的http模块. requestsuperagent非常容易且易于发出远程请求

to make a remote request you can use built-in httpmodule. request and superagent are great and easy to make remote requests

安装这些模块:

npm install request --save
var request = require('request');

npm install superagent --save
var request = require('superagent');

查看更多信息: https://www.npmjs.com/package/request

这篇关于从Express获取Google Maps Geocoding JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆