/timoz-game

Primary LanguageTypeScript

timoz-game

This is a technical demo based on NodeJS and TypeScript

Table of Contents

Installation

Unfortunately, there is no live version to play. but you can install this locally and test this.

  1. use the command below to download the project:
git clone https://github.com/Rahimi0151/timoz-game.git
  1. use the command below to install all the dependencies:
npm install
  1. make sure to have ts-node installed globally by using the code below:
npm install -g ts-node
  1. use the command below to run a version locally
ts-node src/index.ts
  1. if you just want to check if all the unit/integration tests are passing, you can use:
npm test

Core Technologies

Runtime (NodeJS)

the project is based on Node v20.5.1

REST API (ExpressJS)

as a beginner, i used express as a base to develope REST APIs and make the program work

TypeChecking (TypeScript)

After the whole project was done, I watched a course on TypeScript by Mosh Hamedani and converted all the .js files to .ts. to be honest, i mostly used chatGPT to translate these files to TypeScript and put down some finishing touches and removed any potential bugs.

Database (MongoDB)

I used MongoDB as the primary Database for the project. there is no reason for me to use this, just a personal preference. I could have also used any SQL base databases.

I have had experience with raw SQL codes (without any ORM) as shown HERE (this was a collage project. it's not the best code, but it shows i know my way around plain sql)

I also had some experience with Eloquent ORM which was an SQL ORM built into the Laravel Framework as shown in this project

Real Time Connections (Socket.IO)

When the game starts, there will be a new Socket.IO connection between the server and the client to improve the games responsiveness.

Caching (Redis)

As the game is a fun game and has a potential to have multiple thousand players at the same time, i used redis to cache some of the most used data during the match. for example:

  • the questions in the game that is currently being played
  • information about each player that is still playing
  • number of remaining players

Documentation (OpenAPI)

As this is a collaborative industry and we all need to communicate how we have build our APIs, I needed a standard to document my APIs.

So I have learned and used OpenAPI (formerly known as Swagger) to document my endpoints

The reason I used OpenAPI is that it's an industry standard used by large scale companies like ZarinPal (as shown here)

Testing and Test Driven Development (Jest)

As the goal of all the big companies is to make maintainable codes and minimize technical debt, almost all companies use a testing framework.

Almost all the codes in this project are covered by some sort of test, either a unit or an integration test.

You can generate code coverage report. To do so:

  1. install the Jest library globally:
npm install -g jest
  1. run this command to generate
jest --coverage --maxWorkers=1

miscellaneous Libraries

Hashing the Passwords (bcrypt)

As a security best practice, we should never store raw password in pain text. So I used bcrypt library to encrypt/hash the passwords saved in the database.

Configuration/Secrets management (config)

I used config package to store all the secrets inside one file located at /config/default.json.

This file is uploaded to github with some default values, but in production, the app will read the values from environment variables to keep them safe using the /config/custom-environment-variables.json file.

Authentication (jsonwebtoken)

As HTTP is a stateless technology, to keep people logged in and perform authentication, I used jsonwebtoken library to generate JWTs.

Data Validation (yup)

Never trust a user's input. So to validate each request, I have used yup package to validate the user's inputs.

To see these validations in action, just check any of the middlewares inside /src/middleware/validate/