This is a technical demo based on NodeJS
and TypeScript
Unfortunately, there is no live version to play. but you can install this locally and test this.
- use the command below to download the project:
git clone https://github.com/Rahimi0151/timoz-game.git
- use the command below to install all the dependencies:
npm install
- make sure to have
ts-node
installed globally by using the code below:
npm install -g ts-node
- use the command below to run a version locally
ts-node src/index.ts
- if you just want to check if all the unit/integration tests are passing, you can use:
npm test
the project is based on Node v20.5.1
as a beginner, i used express as a base to develope REST
APIs and make the program work
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.
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
When the game starts, there will be a new Socket.IO connection between the server and the client to improve the games responsiveness.
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
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)
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:
- install the Jest library globally:
npm install -g jest
- run this command to generate
jest --coverage --maxWorkers=1
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.
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.
As HTTP is a stateless technology, to keep people logged in and perform authentication, I used jsonwebtoken library to generate JWTs.
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/