As part of my back-end with NodeJS learning journey, I've built a REST API that supports a Dice Game with Express, JWT and MongoDB. Restful API best practices are applied, as following this article on API Architecture.
A second version of the project using OOP (JavasScript Classes) and MySQL can be found here: Rest API for Dice Game (MySQL).
The basic premise of the game is this: each user roll 2 dices, and if the result is 7, the user scores a point. The user that scores more points, wins! Easy peasy!
Here are the main features of the app:
- Login: to play a user must be registered in the app either with a non-repeated name or as 'Anonymous'. Once created, it will be given a JWT Token.
- Results: a player can see his results, including the result of each roll and his own success rate. A player can start over by removing all his rolls.
- Overall Ranking: the app shows the overall ranking, including all players data, overall success rate, and each player's success rate.
- Authorization: JWT is implemented as a middleware in order to allow the user to access routes, services, and resources that are permitted with the give token.
This is the tech stack I've worked with:
To get a local copy up and running follow these simple steps.
- Clone the repo
git clone https://github.com/jempico/dice-game-nodejs.git
- Install NPM packages
npm install
- Add environment variables: edit or create and
.env
file in the root directory with the following data:PORT=3000 DB_USER = tester DB_PASSWORD = AWnHq3IDZaeDL8DP SECRET_TOKEN_ACCESS = 3d9683dc562b1e28fafec01bf1b4438da8faf35b205adf7049221854076040d879882ebd9c900f71dbf18352d08ae363c5f0f3eacabe40892f0777e9f27f0e93
- Run the app
node app
Some examples of how the request body should look like in order to make a request:
-
POST/players: adds a new player
{ "newData":{ "name": "Laia", "email": "[email protected]", "password": "uniquepassword" } }
-
PUT/players: updates player name
{ "currentData": { "name": "currentName" }, "newData":{ "name": "newName" } }
-
POST/players/:id/games: adds a new game
-
DELETE/players/:id/games: removes games from player with requested ID
-
GET/players/: reads all players
-
GET/players/:id: reads player by id
-
GET/players/ranking: reads ranking and overall success rate
-
GET/players/ranking/loser: reads player with lowest success rate
-
GET/players/ranking/winner: reads player with highest success rate
To test all routes (except POST/player) use an authentication header using Bearer schema
All routes except POST/player:
Authorization: Bearer <token>
- API Architecture best practices: OOP and design patterns, separation of concerns
- Set up a Cloud-Hosted Database with MondoDB Atlas
- Authenticate users with JSON web token.
- Get confident with Async/Await.
- Encrypt user passwords with "bcrypt".
- Use Postman to test all routes.
- Apply Mongoose 'static' methods vs. 'instance' methods.
Jemimah Pico - Portfolio - Linkedin - [email protected]
Project Link: https://github.com/jempico/dice-game-nodejs