May 2018
Intermediate to advanced
470 pages
13h 54m
English
On the homepage of the MERN Marketplace, we will display five of the latest products added to the marketplace. To fetch the latest products, we will set up an API that will receive a GET request at /api/products/latest.
mern-marketplace/server/routes/product.routes.js:
router.route('/api/products/latest') .get(productCtrl.listLatest)
The listLatest controller method will sort the list of products in the database with the created date from newest to oldest and return the first five from the sorted list in the response.
mern-marketplace/server/controllers/product.controller.js:
const listLatest = (req, res) => { Product.find({}).sort('-created').limit(5).populate('shop', '_id name').exec((err, products) => { if (err) { return ...Read now
Unlock full access