Thanks to visit codestin.com
Credit goes to github.com

Skip to content

jempico/dice-game-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Logo

REST API for a Dice Game

A REST API for a Dice Game built with NodeJS, MongoDB, Mongoose, and JWT.

Table of Contents
  1. About The Project
  2. Main Features
  3. Built With
  4. Installation
  5. Endpoints
  6. Authentication
  7. Things I've Learned
  8. Contact

About The Project

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!

Main Features

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.

(back to top)

Built With

This is the tech stack I've worked with:

(back to top)

Installation

To get a local copy up and running follow these simple steps.

  1. Clone the repo
    git clone https://github.com/jempico/dice-game-nodejs.git
  2. Install NPM packages
    npm install
  3. 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
    
  4. Run the app
     node app

(back to top)

Endpoints

Some examples of how the request body should look like in order to make a request:

  1. POST/players: adds a new player

    {
             "newData":{
                 "name": "Laia",
                 "email": "[email protected]",
                 "password": "uniquepassword"
             }
     }
    
  2. PUT/players: updates player name

    {
         "currentData": {
             "name": "currentName"
         },
         "newData":{
             "name": "newName"
         }
     }
    
  3. POST/players/:id/games: adds a new game

  4. DELETE/players/:id/games: removes games from player with requested ID

  5. GET/players/: reads all players

  6. GET/players/:id: reads player by id

  7. GET/players/ranking: reads ranking and overall success rate

  8. GET/players/ranking/loser: reads player with lowest success rate

  9. GET/players/ranking/winner: reads player with highest success rate

(back to top)

Authentication

To test all routes (except POST/player) use an authentication header using Bearer schema

All routes except POST/player:

Authorization: Bearer <token> 

(back to top)

Things I've learned

  • 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.

(back to top)

Contact

Jemimah Pico - Portfolio - Linkedin - [email protected]

Project Link: https://github.com/jempico/dice-game-nodejs

About

REST API for a Dice Game built with Express + MongoDB + JWT + Postman. 🎲🎲

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published