1
NodeJS、Express、およびMongoDB APIにサーバーサイドページネーションを追加しようとしています。サーバーサイドからのマングースページ分割
APIは、mongooseを使用してデータベースを処理します。
コントローラからの応答をカスタマイズする方法がわかりません。
モデル:取得、すべてのクライアントのための
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const clientSchema = Schema({
code: {
type: String,
required: [true,'Code no puede estar vacio']
},
name: {
type: String,
required: [true,'Name no puede estar vacio']
}
},{
timestamps: true
});
const Client = module.exports = mongoose.model('clients',clientSchema);
コントローラ:私は何かに、コントローラに結果をカスタマイズすることができますどのように
app.get('/api/clients', (req, res) => {
Client.index(limit,(err, client) => {
if (err) {
res.status(500).json({
msg: "Error en aplicacion",
err
});
}
res.status(200).json(client);
});
});
:
const mongoose = require("mongoose");
const Client = require('../models/client');
const clientController = {};
clientController.index = (limit, callback) => {
Client.find(callback).limit(limit);
};
module.exports = clientController;
ルートは、クライアントを取得しますこのように:
[
{
"totalRecords":"99999999999",
"offset":"888888",
"page":"4",
"nextPage":"5"
"result":{...}
}
]
私はすでにページネーションを計算する機能を持っていますが、コントローラーの結果にページネーションに関する情報を追加する方法はわかりません。
私は経路にページングデータを追加する前に、コントローラのページングロジックを処理する必要がありました。
また、経路のページネーションを処理する方が良いですか?事前
私が作ったMongoose Repositoryクラスは、おそらくhttps://www.npmjs.com/package/morepo – CreasolDev