Skip to content

agungsptr/node-clean-ts

Repository files navigation


Node Clean
TypeScript

A Node.js project template with clean architecture implementation
Explore the docs »

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Contributing
  5. License
  6. Contact
  7. Acknowledgments
  8. Support Me

About The Project

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.

Features

  • RESTful
  • gRPC
  • Authentication using JWT
  • CRUDS Resources
  • Unit Test
  • Coverage Test
  • Load Test

Built With

  • NodeJS
  • Express
  • Mongodb
  • Docker

Getting Started

Let's start this project...

Prerequisites

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

Installation

  1. Clone the repo

    git clone https://github.com/agungsptr/node-clean-ts.git
  2. Install Yarn packages

    yarn install
  3. Copy .env.example to .env

  4. Setup MongoDB container

    make infra
  5. Seed database

    make seed
  6. Now you can run this project, this project running on port 7000 in your local

    make dev

Alternative Installation

Follow this instruction if you do not use docker or in Windows or if you having trouble with the installation above.

  1. Clone the repo

    git clone https://github.com/agungsptr/node-clean-ts.git
  2. Make sure that you have installed Node, Yarn, and Mongodb in your local

  3. Install Yarn packages

    yarn install
  4. Copy .env.example to .env

  5. Setup Mongodb, for this you need to match your Mongodb 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)
  6. Seed database

    yarn seed
  7. Now you can run this project, this project running on port 7000 in your local

    yarn dev

Usage

Web Services

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

gRPC

  • 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

Unit Test

  make test

The tests carried out include:

  • Data Access (./data-access)
  • Models (./models)
  • Routes (./drivers/webservice/routes)

Screenshots result

Unit Test 1

Unit Test 2

Coverage Test

  make coverage_test

Screenshots result

Coverage Test

Load Test

  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

Load Test 1

Load Test 2

Available Make Commands

# 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

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch ( git checkout -b feature/magic )
  3. Commit your Changes ( git commit -m 'feature/magic: Add some magic' )
  4. Push to the Branch ( git push origin feature/magic )
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Agung Saputra - agung.e.sptr@gmail.com

Project Link: here

Acknowledgments

Resources that help me to build this project.

Support Me

"Buy Me A Coffee" Trakteer Saya