如何将分页应用于 json-server 中的子对象?

How can I apply pagination to a subobject in json-server?

我有一个 json-server,其中包含以下文件:

db.json:

var faker = require('faker');

module.exports = function () {
    var data = {
        slack_users_list: slack_users_list(),
        slack_users_info: slack_users_info()
    }

    return data;
}

function slack_users_list() {
    const data = {
        members: []
    }

    for (var i = 0; i < 10; i++) {
        data.members.push({
            id: "user_id_" + i
        })
    }

    return data;
}

function slack_users_info() {
    const data = {
        user: []
    }

    for (var i = 0; i < 10; i++) {
        data.user.push({
            id: "user_id_" + i,
            name: faker.internet.userName(),
            team_id: faker.datatype.string(),
            profile: {
                title: faker.name.title(),
                first_name: faker.name.firstName(),
                last_name: faker.name.lastName(),
                title: faker.name.title(),
                email: faker.internet.email(),
                phone: faker.phone.phoneNumber(),
                skype: faker.internet.userName(),
            },
        })
    }

    return data;
}

server.js

const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router(require('./db.js')())
const middlewares = jsonServer.defaults()

server.use(middlewares)


server.use(jsonServer.bodyParser)

// Wrap response depending on the object being called.
router.render = (req, res) => {

    if (req.url.startsWith('/slack_users_list')) {
        res.jsonp(res.locals.data)

    } else if (req.url.startsWith('/slack_users_info')) {
        param_user = req.query.user

        if (param_user == null) {
            res.jsonp({
                "ok": false,
                "error": "user_not_found"
            })
            return
        }

        user_list = res.locals.data.user
        user_info = {}
        for (var i = 0; i < user_list.length; i++) {
            if (user_list[i].id == param_user) {
                user_info = user_list[i]
            }
        }
        res.jsonp({ user: user_info })

    } else {
        res.jsonp(res.locals.data)
    }
}

server.use(router)

// Run server on indicated port.
server.listen(port = 3000, () => {
    console.log('JSON Server is running')
    console.log('http://localhost:' + port)
})

所以当我调用 http://localhost:3000/slack_users_list 时,我得到:

{
  "members": [
    {
      "id": "user_id_0"
    },
    {
      "id": "user_id_1"
    },
    {
      "id": "user_id_2"
    },
    {
      "id": "user_id_3"
    },
    {
      "id": "user_id_4"
    },
    {
      "id": "user_id_5"
    },
    {
      "id": "user_id_6"
    },
    {
      "id": "user_id_7"
    },
    {
      "id": "user_id_8"
    },
    {
      "id": "user_id_9"
    }
  ]
}

但是如果我想对其应用分页(例如 http://localhost:3000/slack_users_list?_page=1&_limit=2),我会得到所有相同的对象,我假设是因为分页被应用于对象的第一行而不是每一行在 db.json.

中生成的成员内部元素

我如何修改它以在子对象上应用分页 members

谢谢!

您可以在发送响应之前进行分页。

// !!!
// This is just for demonstration.

// consider this req as the actual request.(req, res)
const req = {
    params: { _page: '2', _limit:'3'}
}

// consider this as actual slack_users_list.members
const members = [
    {
      "id": "user_id_0"
    },
    {
      "id": "user_id_1"
    },
    {
      "id": "user_id_2"
    },
    {
      "id": "user_id_3"
    },
    {
      "id": "user_id_4"
    },
    {
      "id": "user_id_5"
    },
    {
      "id": "user_id_6"
    },
    {
      "id": "user_id_7"
    },
    {
      "id": "user_id_8"
    },
    {
      "id": "user_id_9"
    }
]


const page=Number(req.params._page)
const limit=Number(req.params._limit)

// paginating
const limited = members.slice((page - 1) * limit, page * limit)

console.log(limited)

// // send paginated response
//res.json(limited)