node.jsmongodb 페이지 조회

router.get('/news/list', function (req, res, next) {
        var limit = req.param("limit", 10);
        var currentPage = req.param("currentPage", 1);

        if (currentPage < 1) {
            currentPage = 1;
        }

        News.find({"state": 1}).exec(function (err, rs) {
            if (err) {
                res.send(err);
            } else {
                var totalPage = Math.floor(rs.length / limit);
                if (rs.length % limit != 0) {
                    totalPage += 1;
                }

                if (currentPage > totalPage) {
                    currentPage = totalPage;
                }
                console.log(currentPage);
                var query = News.find({"state": 1});
                query.skip((currentPage - 1) * limit);
                query.limit(limit);
                query.exec(function (err, result) {
                    jsonArray = {totalCount: rs.length, data: result};
                    res.json(jsonArray);
                });
            }
        });
    });

좋은 웹페이지 즐겨찾기