A robust and scalable Node.js template for building RESTful APIs with Express, TypeScript, Prisma, and more.
- Clean Architecture: Organized codebase with clear separation of concerns (Domain, Application, Infrastructure, Presentation).
- Express.js: Fast, unopinionated, minimalist web framework for Node.js.
- TypeScript: Typed superset of JavaScript that compiles to plain JavaScript.
- Prisma: Next-generation ORM for Node.js and TypeScript.
- Zod: TypeScript-first schema validation with static type inference, integrated into Use Cases.
- PostgreSQL: Powerful, open source object-relational database system.
- Docker: Containerization for easy development and deployment.
- Swagger: API documentation and testing.
- ESLint & Prettier: Code linting and formatting.
- Husky & commitlint: Git hooks to enforce commit message conventions.
- Jest: Delightful JavaScript Testing Framework, with unit tests for Use Cases.
- Multer: Middleware for handling
multipart/form-data.
- Node.js
- Express
- TypeScript
- Prisma
- Zod
- PostgreSQL
- Docker
- Swagger
- ESLint
- Prettier
- Husky
- commitlint
- Jest
- tsup
-
Clone the repository:
git clone https://github.com/Aleydon/NodeJs-Template.git cd NodeJs-Template -
Install dependencies:
npm install # or yarn install -
Set up environment variables:
Create a
.envfile in the root of the project and add the following variables:DATABASE_URL="postgresql://docker:docker@localhost:5432/postgres?schema=public" PORT=3333
-
Start the database:
npm run docker:up
-
Run database migrations:
npx prisma migrate dev
-
Start the development server:
npm run dev
The application will be available at http://localhost:3333.
The API documentation is available at http://localhost:3333/docs.
The available routes are:
GET /users: List all users.POST /users: Create a new user.PUT /users/:id: Update a user.DELETE /users/:id: Delete a user.
This template uses Prisma as the ORM and PostgreSQL as the database. The database schema is defined in prisma/schema.prisma.
To create a new migration, run the following command:
npx prisma migrate dev --name <migration_name>This project follows the principles of Clean Architecture, promoting a clear separation of concerns and testability. The main layers are:
- Domain Layer: Contains enterprise-wide business rules and entities (e.g.,
Userentity). - Application Layer: Orchestrates the flow of data to and from the Domain Layer. It contains Use Cases (e.g.,
CreateUserUseCase,UpdateUserUseCase) that encapsulate application-specific business rules. Input validation using Zod is performed in this layer. - Infrastructure Layer: Handles external concerns such as database persistence (e.g.,
PrismaUserRepository), external APIs, and web frameworks (e.g., Express routes). - Presentation Layer: Deals with how the data is presented to the user (e.g.,
UserControllerhandling HTTP requests and responses).
This project uses Jest for testing. Unit tests are primarily focused on the Application Layer (Use Cases) to ensure the core business logic is correct and isolated from external concerns. Dependencies are mocked to achieve true unit testing.
To run the tests, use the following commands:
npm test: Run all tests.npm run test:watch: Run all tests in watch mode.npm run test:coverage: Run all tests and generate a coverage report.
This project uses ESLint and Prettier for code linting and formatting. To run the linter and formatter, use the following commands:
npm run lint: Lint all files.npm run format: Format all files.
This project uses Docker for containerization. To manage the Docker containers, use the following commands:
npm run docker:up: Start the Docker containers.npm run docker:down: Stop the Docker containers.npm run docker:restart: Restart the Docker containers.npm run docker:ps: List all running Docker containers.
This project is licensed under the MIT License. See the LICENSE file for details.