This is a NodeJs application written in Typescript and some enhancement. By applying some design patterns and design architecture, unit test and integration test mechanism against a real database, caching mechanism with Redis, documentation with open API, Docker, CI/CD stuffs, authentication using both JWT and HMAC signature that helps this application consideration as a NodeJS (Typescript) boilerplate.
The idea of creating this application is to handle real-life requirements using a strong Node JS (Typescript) application using some design patterns, architecture, and the proper testing mechanism that helps the application's stability and scalability.
Build a task management API.
- User is able to register through an endpoint
- User is able login through an endpoint after registering
- A user is able to submit a task through an endpoint.
- The backend will consume the task (use a random timer delay to simulate some work).
- Through another endpoint, the user can check the status of the task.
- Store tasks in a database (MySQL)
- A task is first created as a draft with a 24h lifetime, it needs to be confirmed to be added to the task queue.
- Use Redis to store draft tasks
- Create an authentication procedure between the client and the server using either an HMAC signature and a shared secret
Follow these steps below to make sure the application running in the correct way:
- Makes sure you have installed
Mysql,Redis,NodeJs,PM2on your local machine before stating the application - Otherwise, you should use
Docker Containerto host the application.
- Run the command
npm ito install all the application dependencies. - Modify
.env.examplefile to.envand config the corresponding information into.envfile - Run the command
npm run lintto checking the coding convention is strict. - Run the command
npm run formatto format the Typescript code styles. - Run the command
npm run migration:createto create a database migration file. - Run the command
npm run migration:runto run all the current database migration. - Run the command
npm run migration:revertto revert the current database migration. - Run the command
npm run .to starting development the application. - Run the command
npm run buildto compiler the application from TS to JS. - Run the command
npm run start:debugto enable debug mode. - Run the command
npm run testto performs the all the test. - Run the command
npm run test:composeto performs the all the test with Docker.
Run the commands below to build the application production mode:
First we need to change NODE_ENV=production in the .env file
- Run the command
npm run startto run application on production mode. - Run the command
npm run start:prodto build application with PM2 cluster. - Run the command
npm run stopto stop current application running on PM2 cluster. - Run the command
npm run deleteto delete current application running on PM2 cluster. - After running
npm run start:prodthe application will migration database automatically in production environment.
Run the application in Docker Environment:
- Make sure your local machine has been installed the Docker container.
- Run the command
docker-compose upto starting development the application. - Run the command
docker-compose downto stop development the application. - Run the command
docker-compose up --buildto rebuild the application. - Run the command
docker-compose up -dto build the application in the background. Note*: There are some other docker command need to hand on by your self.
- Change open api
OPEN_API_USERNAMEandOPEN_API_PASSWORDto what ever characters in.envfile - Otherwise, the credentials will set as default as shown below:
- Username:
task - Password:
management
- Username:
- Landing on
http://localhost:3005/api/docsto open the Open API docs for local environment. - Landing on
{url}/api/docsto open the Open API docs for development/production environments. - Use the default credentials or username/password setting up in the
.envfile for sign in to open API.