Controller 

The :gameId param in the request to the read API will call the gameByID controller method, which is similar to the userByID controller method. It will retrieve the game from the database and attach it to the request object to be used in the next method.

mern-vrgame/server/controllers/game.controller.js:

const gameByID = (req, res, next, id) => {  Game.findById(id).populate('maker', '_id name').exec((err, game) => {    if (err || !game)      return res.status('400').json({        error: "Game not found"      })    req.game = game    next()  })}

The next method, in this case the read controller method, simply returns this game object in the response to the client.

mern-vrgame/server/controllers/game.controller.js:

const read = (req, res) => { return res.json(req.game) ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.