【发布时间】:2022-11-15 19:06:13
【问题描述】:
我不知道这是否是最好的询问地点,但是。
我正在构建一个天气应用程序,它使用axios 使用 api,然后使用 express 提供服务。我想知道应该在哪里添加缓存以提高 api 的速度?是在消费的时候在axios层还是在服务的时候在快递层。
下面是我的一些上下文代码
import { weatherApiKey } from 'config';
import axios from 'axios';
const forecast = (location, service) => {
console.log('inside api calling location: ', location);
axios.get(`http://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${weatherApiKey}`)
.then(res => {
service(undefined, res.data)
})
.catch(err => {
service('Error calling weather API');
})
}
module.exports = forecast;
然后我通过以下方式提供消耗的 api。
app.get('/weather', (req, res) => {
const locale = req.query.locale;
if(!locale) {
return res.send({
error: 'Please provide valid locale'
})
}
foreCast(locale, (err, weatherData) => {
if(err) {
console.log('error in calling weather API')
res.send({err});
}
console.log('returning weather data', weatherData)
res.send({weatherData})
});
})
【问题讨论】:
标签: node.js express caching axios