April 2020
Intermediate to advanced
716 pages
18h 55m
English
In order to implement a read course API in the backend, we will start by declaring the GET route and the parameter-handling trigger, as shown in the following code.
mern-classroom/server/routes/course.routes.js:
router.route('/api/courses/:courseId') .get(courseCtrl.read)router.param('courseId', courseCtrl.courseByID)
We are adding this GET route to query the Course collection with an ID and return the corresponding course in the response. The :courseId param in the route URL will call the courseByID controller method, which is similar to the userByID controller method. It retrieves the course from the database, and attaches it to the request object that is to be used in the next method, as shown in the following code.
Read now
Unlock full access