This project implements a clean architecture approach for a Node.js TypeScript application. It follows the principles of separation of concerns, dependency inversion, and testability.
The project aims to provide a robust and scalable foundation for building Node.js applications using TypeScript. It leverages clean architecture principles to separate concerns into layers: domain, application, and infrastructure. This separation allows for better maintainability, testability, and flexibility.
The project structure follows a modular approach:
src/
app/
: Contains the application layer, including services, repositories, and routes.infrastructure/
: Houses infrastructure concerns such as server setup, database configuration, logging, and external service integration.server.ts
: Initializes the Express server and middleware configurations.database/
: Manages database connections and schemas.index.ts
: Connects to the database using Mongoose and sets up logging.
logger/
: Handles logging functionality.winston.ts
: Configures and exports a Winston logger with colorized output and file transport for error logging.
redis/
: Handles Redis connection and client setup.createRedisClient.ts
: Defines a function to create and configure a Redis client.
app/domain/
: Defines the domain entities and business logic.app/middlewares/
: Middleware functions for request validation and authentication.app/models/
: Defines the data models used in the application.app/repositories/
: Contains interfaces and implementations of repositories for data access.app/routes/
: Defines the API routes.
To get started with the project, follow these steps:
-
Clone the repository:
git clone <repository-url>
-
Install dependencies:
cd <project-folder> npm install
-
Set up environment variables:
Create a
.env
file in the root of the project and define the necessary environment variables, such asPORT
,DB_URI
,SECRET
, etc. -
Run the application:
npm start
Once the application is running, you can access the API endpoints as described below.
- POST /api/user: Register a new user.
- POST /api/user/login: Login with existing user credentials and receive an access token.
- GET /api/user/:id: Get user details by ID.
- GET /api/users: Get all users.
- PUT /api/user: Update user details.
- DELETE /api/user/:id: Delete a user.
- GET /healthcheck: Check the health of the application.
Contributions are welcome! Feel free to open issues or pull requests for any improvements or new features you'd like to see.
This project is licensed under the MIT License.