0
我一直在试图做两个链接的HTTP请求调用,第二个调用是基于第一个调用的返回结果。要求是第一次打电话获取IP信息并使用IP信息进行第二次呼叫以获取天气数据。我正在使用节点模块koa-http-requestChaning与Koa.js的两个HTTP请求
它只适用于一个请求,要么我只能得到IP或我只能得到天气数据。
var koa = require('koa');
var koaRequest = require('koa-http-request');
var app = new koa();
var lat = '';
var log = '';
// app.use(koaRequest({
// dataType: 'json', //automatically parsing of JSON response
// timeout: 3000, //3s timeout
// host: 'http://ip-api.com'
// }));
//
// app.use(async (ctx, next) => {
// var ipObject = await ctx.get('/json', null, {
// 'User-Agent': 'koa-http-request'
// });
// lat = parseInt(ipObject.lat);
// lon = parseInt(ipObject.lon);
// });
app.use(koaRequest({
dataType: 'json', //automatically parsing of JSON response
timeout: 3000, //3s timeout
host: 'http://api.openweathermap.org/'
}), next);
app.use(async (ctx, next) => {
var weatherObject = await ctx.get('/data/2.5/weather?lat=43&lon=-79&appid=***', null, {
'User-Agent': 'koa-http-request'
});
console.log(ctx.request);
ctx.body = weatherObject;
});
app.listen(process.env.PORT || 8090);
谢谢塞巴斯蒂安!我也从提供类似代码的作者那里得到了回复。 –