Search⌘ K
AI Features

Adding Routes to Get a Single Movie and its Ratings

Explore how to add backend routes in Node.js and Express to fetch a single movie along with its reviews and to obtain all movie ratings. Understand using URL parameters, MongoDB aggregation with $lookup, and testing API endpoints effectively.

In this lesson, we’ll add two more routes—a route to get a specific movie (with its reviews) and a route to get the ratings of all the movies in the database.

MOVIEREVIEWS_DB_URI=mongodb+srv://Cluster09422:XVNEenFBU3N8@cluster09422.yqome88.mongodb.net/sample_mflix?retryWrites=true&w=majority
MOVIEREVIEWS_NS=sample_mflix
PORT=3000
The initial files we will build on

In the movies.route.js route file, add the two routes shown below:

Javascript (babel-node)
router.route('/').get(MoviesController.apiGetMovies)
router.route("/id/:id").get(MoviesController.apiGetMovieById)//route to get a specific movie
router.route("/ratings").get(MoviesController.apiGetRatings) // route to get all ratings

Line 2: This route retrieves a specific movie and all reviews associated with that movie.

Line 3: This route returns a list of movie ratings (such as G, PG, R) so that a user can select the ratings from a dropdown menu in the frontend.

Retrieving movies by ID and rating

Next, let’s implement the apiGetMovieById and apiGetRatings methods in ...