json 如何在node,express js中获取以“ka”开头的API和过滤器名称

j0pj023g  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(115)
const express = require('express')
const app = express()
const fetch = require('node-fetch');

const url = "https://jsonplaceholder.typicode.com/users";

let users = [];
function getUsers(url) {
    const user = fetch(url)
        .then((res) => {
            res.json().then((res1) => {
                users = res1;
            })
        })
};

const  filterUser = (req,res) => {
    const filteredUser = users.filter(user => user.name.toLowerCase().startsWith('ka'))
    let fresults = res.json(filteredUser)
    return fresults;
}

app.get('/', (req, res) => {
    res.send(users)
})

app.listen(3000, () => {
    console.log("listening on port 3000");
    getUsers(url)
})

字符串
https://jsonplaceholder.typicode.com/users获取用户数据的url,并从用户名'Ka'开始过滤用户,并打印输出列表

7xllpg7q

7xllpg7q1#

您需要获取用户并将其过滤掉。您没有在任何端点中调用它

const url = "https://jsonplaceholder.typicode.com/users";

const getUsers = async (filter) => {
  const resp = await fetch(url);
  const users = await resp.json();
  return filter ? users.filter(filter) : users;
}

const filterUserNameKa = (user) => user.username.toLowerCase().startsWith('ka');

app.get('/', async (req, res) => {
    const users = await getUsers(filterUserNameKa);
    res.send(users)
})

字符串

相关问题