NodeJS - 来自 app.js 的第三方 Api 调用

NodeJS - Third party Api call from app.js

我需要在 NodeJS 中从后端调用第 3 方 api 并且 return 数据到 ajax 在前端调用

下面是我的代码:

router.post('/get_data', function(request, response){
var city_name = request.body.city_name;
if(city_name in city_name_done){

}
else {
    city_name_done.push(city_name);
    console.log('city_name: ' + city_name);
    var options = {
        host : 'api.openweathermap.org',
        path : '/data/2.5/forecast/daily?q=' + city_name + '&mode=json&units=metric&cnt=14&appid=75e843de569fb57a783c2e73fd9a7bb5',
        method : 'GET'
    }
    var maybe = '';
    console.log('till here')
    var req = http.request(options, function(res){
        var body = "";
        res.on('data', function(data) {
            console.log('data came');
            body += data;
        });
        res.on('end', function() {
            console.log('ended too');
            maybe = JSON.parse(body);
            console.log(maybe.city);
        });
    });
    console.log('here too man');
    req.on('error', function(e) {
        console.log('Problem with request: ' + e.message);
    });
    response.send(maybe);
}
});

我能够从前端的 ajax post 请求中获取 city_name 参数,但是我每次执行 post 请求时都会收到 500 内部服务器错误

PS: 请原谅我的英语,甚至是问题的水平,因为我是 NodeJS 的绝对初学者

您正在 return 从从第三方 api 获取数据的函数调用外部响应。您需要 return 来自 res.on('end' 部分的回复。这是因为 api 调用是异步的,我们必须等待响应。

res.on('end', function() {
     console.log('ended too');
     maybe = JSON.parse(body);
     console.log(maybe.city);
     response.send(maybe);
});

完整代码为

    router.post('/get_data', function(request, response){
var city_name = request.body.city_name;
if(city_name in city_name_done){

}
else {
    city_name_done.push(city_name);
    console.log('city_name: ' + city_name);
    var options = {
        host : 'api.openweathermap.org',
        path : '/data/2.5/forecast/daily?q=' + city_name + '&mode=json&units=metric&cnt=14&appid=75e843de569fb57a783c2e73fd9a7bb5',
        method : 'GET'
    }
    var maybe = '';
    console.log('till here')
    var req = http.request(options, function(res){
        var body = "";
        res.on('data', function(data) {
            console.log('data came');
            body += data;
        });
        res.on('end', function() {
            console.log('ended too');
            maybe = JSON.parse(body);
            console.log(maybe.city);
            response.send(maybe);
        });
    });
    console.log('here too man');
    req.on('error', function(e) {
        console.log('Problem with request: ' + e.message);
    });
    res.end();
}
});

用 Postman 做一个简单的 'GET' 到 api.openweathermap.org/data/2.5/forecast/daily?q=Miami&mode=json&units=metric&cnt=14&appid=75e843de569fb57a783c2e73fd9a7bb5 会得到 return 结果。

看来您可能遗漏了 var http = require('http'); 之类的东西 :)

我强烈建议使用 npm 中的 request 模块,这样可以更轻松地处理请求。参见 this

首先:这里不需要maybe。你从 API 得到一个 JSON 字符串,然后你将它解析成一个对象,但是当你发送它时,你需要再次将它字符串化。所以只要坚持使用字符串形式的传入数据即可。

其次:response.sendend 事件之外被调用:所以发生的事情是 - JavaScript 是异步的 - 它会调用 API,但是不要等待响应返回,因为您自己已经向您的客户发送了响应。您需要将其放入 end 回调中,以便实际等待 API.

所以这就是我要做的:

router.post('/get_data', function(request, response){
var city_name = request.body.city_name;
if(city_name in city_name_done){
    return;
}
else {
    city_name_done.push(city_name);
    console.log('city_name: ' + city_name);
    var options = {
        host : 'api.openweathermap.org',
        path : '/data/2.5/forecast/daily?q=' + city_name + '&mode=json&units=metric&cnt=14&appid=75e843de569fb57a783c2e73fd9a7bb5',
        method : 'GET'
    }
    console.log('till here')
    var req = http.request(options, function(res){
        var body = "";
        res.on('data', function(data) {
            console.log('data came');
            body += data;
        });
        res.on('end', function() {
            console.log('ended too');
            console.log(maybe.city);
            response.send(body);
        });
    });
    console.log('here too man');
    req.on('error', function(e) {
        console.log('Problem with request: ' + e.message);
    });
}
});