Skip to content

Instantly share code, notes, and snippets.

@ctsstc
Forked from jaouadballat/productsRoutes
Last active March 24, 2018 08:20
Show Gist options
  • Save ctsstc/f0c4481d92e190ee2687b3caa1f5e13b to your computer and use it in GitHub Desktop.
Save ctsstc/f0c4481d92e190ee2687b3caa1f5e13b to your computer and use it in GitHub Desktop.
const express = require('express');
const router = express.Router();
const Product = require('../models/Product');
router.get('/', function (req, res, next) {
let perPage = 3;
let page = parseInt(req.query.page) || 0;
let pages = 0;
let nextUrl = '';
let prevUrl = '';
Product.count().exec(function (err, count) {
Product.find()
.limit(perPage)
.skip(perPage * page)
.exec(function (err, products) {
pages = Math.floor(count / perPage);
let prevUrl = `http://localhost:3000/products?page=${page - 1}`;
let nextUrl = `http://localhost:3000/products?page=${page + 1}`;
let response = {
products,
currentPage: page,
pages,
count,
prevUrl: '',
nextUrl: ''
};
if (page === 0) {
Object.assign(response, {nextUrl});
} else if (page === pages - 1) {
Object.assign(response, {prevUrl});
} else if (page > 0 && page < pages) {
Object.assign(response, {nextUrl, prevUrl});
} else {
res.redirect('/products')
}
res.json(response);
});
});
});
router.get('/:id', function (req, res, next) {
Product.findById(req.params.id, function (err, product) {
if (err) return console.log(err);
res.status(200).json(product);
})
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment