Node.JS、Express 和 Axios 的 SWAPI 分页

SWAPI Pagination with Node.JS, Express and Axios

我想获取全部 people/films/etc。来自 SWAPI.

我尝试了很多东西才终于得到可用的东西。但是,它只显示前 10 个人(在 people 的情况下)

const express = require("express");
const router = express.Router();
const axios = require("axios");

router.get("/people", (req, res) => {
  axios
    .get("https://swapi.dev/api/people/?format=json")
    .then((data) => {
      return res.send(data.data.results);
    })
    .catch((error) => {
      console.log("error: ", error);
    });
});

module.exports = router;

感谢 Stack Overflow 的大量搜索,我尝试的是:

const express = require("express");
const router = express.Router();
const axios = require("axios");

router.get("/people", async (req, res) => {

  let nextPage = `https://swapi.dev/api/people/`;
  let people = [];
  while (nextPage) {
    res = await axios(nextPage)
    const { next, results } = await res.data;

    nextPage = next

    people = [...people, ...results]
  } 
  console.log(people.length) // 82
  console.log(people) // shows what I wanted, all people !
  return people;


});

module.exports = router;

启动服务器时,页面没有完成加载(此时仍在加载),但console.log设法显示了我想要的内容。

那么,我怎样才能在页面中显示它呢?

我的目标是将该路由用于来自 React 前端的 axios API 调用(搜索特定名称)

不要覆盖代码中的 res 并以 res.send:

结束
let nextPage = `https://swapi.dev/api/people/`;
let people = [];
while (nextPage) {
  let nextres = await axios(nextPage)
  const { next, results } = await nextres.data;
  nextPage = next
  people = [...people, ...results]
} 
console.log(people.length) // 82
console.log(people) // shows what I wanted, all people !
res.send(people);