This is a Node.js backend template built with TypeScript, designed to help you quickly bootstrap scalable and maintainable backend applications. It includes features like authentication, role-based access control, logging, caching, and support for multiple databases (MySQL, PostgreSQL, MongoDB).
- TypeScript: Strongly typed code for better maintainability.
- Clean Architecture: Separation of concerns with controllers, services, and repositories.
- Authentication: JWT-based authentication with role and permission management.
- Logging: Integrated logging using Winston.
- Caching: Support for Redis or in-memory caching.
- Multiple Databases: Support for MySQL, PostgreSQL, and MongoDB.
- Swagger Documentation: Auto-generated API documentation.
- Error Handling: Centralized error handling middleware.
- Environment Variables: Configuration via .env file.
- Export Utilities: Export data to PDF, Excel, and TXT formats.
Follow these steps to set up and run the project locally.
- Node.js (v16 or higher)
- pnpm (v7 or higher)
- MySQL/PostgreSQL/MongoDB (optional, depending on your database choice)
-
Clone the repository:
git clone https://github.com/your-username/template-node-backend-ts.git cd template-node-backend-ts -
Install dependencies:
pnpm install
-
Create a .env file in the root directory and add the required environment variables:
PORT=3000 JWT_SECRET=your_jwt_secret MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_USER=root MYSQL_PASSWORD=yourpassword MYSQL_DB=template_db
-
Run the project in development mode:
pnpm dev
-
Access the API documentation at:
http://localhost:3000/api-docs
src/
├── config/ # Configuration files (DB, logger, cache, etc.)
├── controllers/ # Controllers (request/response handling)
├── services/ # Services (business logic)
├── repositories/ # Repositories (data access layer)
├── middleware/ # Middleware (authentication, validation, etc.)
├── models/ # Data models (interfaces, types)
├── routes/ # API routes
├── utils/ # Utility functions (helpers, export utilities)
├── interfaces/ # Global interfaces
├── types/ # Global types
├── app.ts # Express app configuration
└── server.ts # Server entry point
- POST
/auth/login: Authenticate a user and generate a JWT. - GET
/auth/profile: Get the profile of the authenticated user.
- GET
/trips: Get a list of all trips. - POST
/trips: Create a new trip. - GET
/trips/:id: Get a trip by ID. - PUT
/trips/:id: Update a trip by ID. - DELETE
/trips/:id: Delete a trip by ID.
| Variable | Description | Default Value |
|---|---|---|
| PORT | Port on which the server runs | 3000 |
| JWT_SECRET | Secret key for JWT signing | - |
| MYSQL_HOST | MySQL database host | localhost |
| MYSQL_PORT | MySQL database port | 3306 |
| MYSQL_USER | MySQL database user | root |
| MYSQL_PASSWORD | MySQL database password | - |
| MYSQL_DB | MySQL database name | template_db |
pnpm dev-
Build the project:
pnpm build
-
Start the server:
pnpm start
To run tests, use the following command:
pnpm testThe project includes utilities to export data to PDF, Excel, and TXT formats. Use the following endpoints:
- POST
/export/pdf: Export data to PDF. - POST
/export/excel: Export data to Excel. - POST
/export/txt: Export data to TXT.
- Open a pull request.
- Express - Fast, unopinionated, minimalist web framework for Node.js.
- TypeScript - JavaScript with syntax for types.
- Swagger - API documentation tool.
- Winston - Logging library for Node.js.