Game APIs
The backend in the MERN VR Game will expose a set of CRUD APIs for creating, editing, reading, listing, and deleting games from the database, which can be used in the frontend of the application, including in the React 360 game implementation, with fetch calls.
The create API
A user who is signed in to the application will be able to create new games in the database using the create
API.
Route
In the backend, we will add a POST
route in game.routes.js
, that verifies that the current user is signed in and authorized, and then creates a new game with the game data passed in the request.
mern-vrgame/server/routes/game.routes.js
:
router.route('/api/games/by/:userId') .post(authCtrl.requireSignin,authCtrl.hasAuthorization, gameCtrl.create)
To process the :userId
param and retrieve the associated user from the database, we will utilize the userByID
method from the user controller. We will also add the following to the game routes, so the user is available in the request
object as profile...