NodeJS-来自app.js的第三方Api调用 [英] NodeJS - Third party Api call from app.js

查看:75
本文介绍了NodeJS-来自app.js的第三方Api调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从NodeJS的后端调用第3方api,并将数据返回到前端的ajax调用

I need to call 3rd party api from backend in NodeJS and return the data to ajax call in frontend

下面是我的代码:

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发布请求中获取city_name参数,但是每次执行发布请求时,我都会收到500个内部服务器错误

I am able to get the city_name parameter from ajax post request from frontend side however I get 500 internal server error everytime I execute the post request

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

PS: Pardon my English and even the level of the question as I am an absolute beginner in NodeJS

推荐答案

您正在从函数调用外部返回响应,该函数调用从第3方api获取数据.您需要从 res.on('end'"部分返回响应.这是因为api调用是异步的,我们必须等待响应出现.

You are returning response from outside of function call that gets data from 3rd party api. You need to return response from res.on('end' section. It is because api call is asynchronous and we have to wait for response to come.

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

这篇关于NodeJS-来自app.js的第三方Api调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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