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

Javascript NodeJS-来自app.js的第三方Api调用,javascript,ajax,node.js,post,http-post,Javascript,Ajax,Node.js,Post,Http Post,我需要在NodeJS中从后端调用第三方api,并将数据返回到前端的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:

我需要在NodeJS中从后端调用第三方api,并将数据返回到前端的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的绝对初学者

您正在从从第三方api获取数据的函数调用外部返回响应。您需要从('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();
}
});

很好,与邮递员进行一次简单的“获取”,以
api.openweathermap.org/data/2.5/forecast/daily?q=Miami&mode=json&units=metric&cnt=14&appid=75e843de569fb57a783c2e73fd9a7bb5
返回结果


因此,看起来您可能缺少了类似
var http=require('http');
maybe:)

我强烈建议使用
npm
中的
request
模块,使请求更容易处理。看

首先:
这里可能不需要。您从API中获得一个
JSON
字符串,然后将其解析为一个对象,但是当您发送它时,您需要再次对其进行字符串化。所以只需使用字符串形式的传入数据

其次:
response.send
end
事件之外被调用:所以发生的事情是——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);
    });
}
});

是否定义了路径/获取数据?为什么不在服务器端使用axios?hello@Zohaib Ijaz你能回答我的问题吗?我的问题是,当我点击第三方api时,它不会发送错误或数据之类的响应。如何在我的node js应用程序中处理这种情况。