April 2020
Intermediate to advanced
716 pages
18h 55m
English
In order to implement the read shop API in the backend, we will start by adding a GET route that queries the Shop collection with an ID and returns the shop in the response. The route is declared along with a route parameter handler, as shown in the following code:
mern-marketplace/server/routes/shop.routes.js:
router.route('/api/shop/:shopId') .get(shopCtrl.read)router.param('shopId', shopCtrl.shopByID)
The:shopId param in the route URL will invoke the shopByID controller method, which is similar to the userByID controller method. It retrieves the shop from the database and attaches it to the request object to be used in the next method. The shopByID method is defined as follows:
mern-marketplace/server/controllers/shop.controller.js ...
Read now
Unlock full access