A boilerplate/starter project to build REST APIs using Node.js with Typescript.
Some boilerplates are too customized, here I tried to build a less complex but good structure with essentials. The sources I have used and I have taken some code samples are here.
- Structure by Components see
- Dependency Injection using typedi
- Validation Layer with class-validator, using dto and custom middleware
- MongoDB object data modeling with mongoose
- Rate Limiting with express-rate-limit
- Monitoring with express-status-monitor
- Events - Subscribers using event-dispatch
- Continuous Integration using circleci
- Role based authorization
- Error Handling centralized
- Linting using eslint and prettier
- Logging with winston
All available routes:
User routes:
POST /users
- create a user
GET /users
- get all users with pagination [admin roles]
GET /users/:userId
- get user [admin roles]
PUT /users/:userId
- update user [admin roles]
DELETE /users/:userId
- delete user [admin roles]
GET /users/me
- get current user
PUT /users/me
- update current user
Auth routes:
POST /auth/login
- login with email and password
POST /auth/reset-password
- create reset code and send with email
POST /auth/reset-password/verify
- verify reset code
POST /auth/reset-password/change
- change password after verify reset code
Folder structure
src
├── api
│ ├── components
│ │ ├── auth
│ │ └── users
│ │ ├── dto # Data Transfer Objects
│ │ ├── interfaces # Component level interfaces
│ │ ├── models # Mongoose models
│ │ ├── subscribers # Event subscribers
│ │ ├── users.service.ts # Business logic
│ │ ├── users.controller.ts # Route controllers
│ │ └── users.routes.ts # Routes
│ │
│ ├── middlewares # Custom express middlewares
│ └── routes.ts # All component routes
│
├── config # Environment variables and configuration
├── constants # Constants such as roles, enums vs.
├── decorators # Custom decorators
├── interfaces # General interfaces
├── jobs # Cron jobs
├── loaders # All loaders to start server
├── types # Custom type definitions
├── uploads # Updloaded files
├── utils # Utility functions
└── main.ts # Entry Point
All structure
src
├── api
│ ├── components
│ │ ├── auth
│ │ │ ├── auth.controller.ts
│ │ │ ├── auth.routes.ts
│ │ │ ├── auth.service.ts
│ │ │ ├── dto
│ │ │ │ ├── change-password.dto.ts
│ │ │ │ ├── create-reset-code.dto.ts
│ │ │ │ ├── login-user.dto.ts
│ │ │ │ └── verify-reset-code.dto.ts
│ │ │ ├── interfaces
│ │ │ │ └── auth.interface.ts
│ │ │ └── models
│ │ │ └── password-reset.model.ts
│ │ └── users
│ │ ├── dto
│ │ │ ├── create-user.dto.ts
│ │ │ ├── get-users.dto.ts
│ │ │ ├── update-current-user.dto.ts
│ │ │ └── update-user.dto.ts
│ │ ├── interfaces
│ │ │ └── user.interface.ts
│ │ ├── models
│ │ │ └── user.model.ts
│ │ ├── subscribers
│ │ │ ├── events.ts
│ │ │ └── user.subscriber.ts
│ │ ├── users.controller.ts
│ │ ├── users.routes.ts
│ │ └── users.service.ts
│ ├── middlewares
│ │ ├── validate.middleware.ts
│ │ ├── verify-access.middleware.ts
│ │ ├── verify-auth.middleware.ts
│ │ └── wrap-async.middleware.ts
│ └── routes.ts
├── config
│ └── index.ts
├── constants
│ ├── auth.ts
│ ├── enums.ts
│ └── index.ts
├── decorators
│ └── event-dispatcher.decorator.ts
├── interfaces
│ └── index.ts
├── jobs
├── loaders
│ ├── dependency-injector.ts
│ ├── events.ts
│ ├── express.ts
│ ├── index.ts
│ ├── jobs.ts
│ ├── logger.ts
│ ├── mailer.ts
│ ├── mongoose.ts
│ ├── monitor.ts
│ └── post-startup.ts
├── main.ts
├── types
│ └── express
│ └── index.d.ts
├── uploads
└── utils
├── delete-undefined-props.util.ts
└── generate-date-range-from-now.util.ts
- Node.js
- MongoDB database
- SMTP server for mail
Copy .env.example to .env and enter your information
cp .env.example .env
Then install modules and start server
npm install
npm run dev
Resources for inspiration and some code samples