我需要在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的绝对初学者答案 0 :(得分:1)
您正在从函数调用之外返回响应,该响应从第三方API获取数据。您需要从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);
});
}
});
答案 1 :(得分:0)
使用Postman到api.openweathermap.org/data/2.5/forecast/daily?q=Miami&mode=json&units=metric&cnt=14&appid=75e843de569fb57a783c2e73fd9a7bb5
做一个简单的'GET'确实会返回结果。
所以看起来你可能会遗漏像var http = require('http');
这样的东西:)
答案 2 :(得分:0)
我强烈建议您使用request
中的npm
模块,使请求更容易处理。请参阅this。
首先:此处不需要maybe
。您从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);
});
}
});