我正在使用NodeJs消费外部API。我感到困惑,因为我认为API只能从前端进行调用。我得到数据的唯一方法是通过我创建的另一个API返回,但我不知道这是否是最好的方法。还有其他方法吗?
这就是我获取URL所需的“id”的方法。
创建第二个API来返回数据是一个好的解决方案吗?
这就是我获取URL所需的“id”的方法。
const express = require('express');
const axios = require('axios');
const app = express();
app.get('/example/:id', (req, res) => {
const { id } = req.params;
const url = `https://theexternalapiurl/${id}`;
const config = { token }
axios.get(url, config).then(response => {
//here I should query a database and return a response
console.log(response.data);
res.json(data);
});
});
创建第二个API来返回数据是一个好的解决方案吗?