A Node.js project template with clean architecture implementation
Explore the docs »
Table of Contents
This a TypeScript version of Node Clean, if you want to see the JavaScript version follow this way.
This project aims to be used as a base backend project which implements clean architecture. I create this project to help me build a backend application faster as a template project. Feel free to use this project as your base application. I am very happy if you are helped by using this template.
- RESTful
- gRPC
- Authentication using JWT
- CRUDS Resources
- Unit Test
- Coverage Test
- Load Test
Let's start this project...
Before you dig into this project it very nice if you understand the clean architecture concept. I recommend you to read about the Clean Architecture first.
- Basic javascript
- VS Code (Recommend)
- Node >= v16.15.0
- Yarn
- GNU Make
- Docker
-
Clone the repo
git clone https://github.com/agungsptr/node-clean-ts.git
-
Install Yarn packages
yarn install
-
Copy
.env.example
to.env
-
Setup
MongoDB
containermake infra
-
Seed database
make seed
-
Now you can run this project, this project running on port 7000 in your local
make dev
Follow this instruction if you do not use docker
or in Windows
or if you having trouble with the installation above.
-
Clone the repo
git clone https://github.com/agungsptr/node-clean-ts.git
-
Make sure that you have installed
Node
,Yarn
, andMongodb
in your local -
Install Yarn packages
yarn install
-
Copy
.env.example
to.env
-
Setup
Mongodb
, for this you need to match yourMongodb
to the.env
file. To do that, please update the followings variable- MONGO_HOST (your MongoDB host, ex. localhost if using your local machine) - MONGO_PORT (your MongoDB port) - MONGO_DBNAME (database name, you need to create it first in MongoDB) - MONGO_USER (username to access your MongoDB) - MONGO_PW (password for the user)
-
Seed database
yarn seed
-
Now you can run this project, this project running on port 7000 in your local
yarn dev
- Login
curl --location --request POST 'localhost:7000/api/auth/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "agungsptr",
"password": "24434"
}'
- Get All Users
curl --location --request GET 'localhost:7000/api/users' \
--header 'Authorization: Bearer <AuthToken>'
For more request examples please use this postman collection here.
- Start gRPC Server
make grpc
After running the gRPC server, you can run example gRPC client, using this command
- Start gRPC Client
make grpc_client
make test
The tests carried out include:
- Data Access (./data-access)
- Models (./models)
- Routes (./drivers/webservice/routes)
Screenshots result
make coverage_test
Screenshots result
make load_test
After you run load test command above you can see the test result in graphic, for that please open html file in test/output/report.html
.
Screenshots result
# Infrastructure
make infra # Run only db container
make build # Build image
make compose-up # Run all container including db and app
make compose-down # Stop all containers
make purge # Delete all images
make auto # Auto command, to build image and run all containers
make wait-db # Check db is ready
make wait-app # Check app is ready
# Application
make start # Run webservice
make dev # Run webservice in dev mode with watching mode
make grpc # Run gRPC
make grpc_client # Run example gRPC client
# Database
make seed # Seeding database
# Testing
make test # Unit testing
make coverage_test # Coverage testing
make load_test # Load testing, to testing the app performace
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/magic
) - Commit your Changes (
git commit -m 'feature/magic: Add some magic'
) - Push to the Branch (
git push origin feature/magic
) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.
Agung Saputra - agung.e.sptr@gmail.com
Project Link: here
Resources that help me to build this project.
- Clean Architecture Node.js - Build a REST API
- Clean Architecture in ExpressJS Applications (NodeJS)
- JavaScript dependency injection in Node – friend or foe?
- Patterns — Generic Repository with Typescript and Node.js
- Express.js
- MongoDB Documentation
- Mongoose Documentation
- gRPC Intoduction
- gRPC Node Quick Start
- Understanding Protocol Buffers
- Official TypeScrip Doc
- Learn Typescript at freecodecamp