A Boilerplate written with TypeScript on Node.js runtime to achieve scalability, maintainability, and simplicity of coding
MEN-BOILERPLATE is a boilerplate written with TypeScript on Node.js runtime depending on the dependency injection pattern to achieve scalability, maintainability, and simplicity of coding. It is a boilerplate for building scalable server-side applications, using modern technologies such as TypeScript, Node.js, Express, Redis, and MongoDB.
MEN Boilerplate includes essential features that every API requires, such as Mailer, SMS, Authentication, Authorization, Notifications, Payments, Subscriptions, and more.
Say goodbye to weeks of setup and coding. This ready-to-use boilerplate empowers you to jumpstart your project and prioritize what makes your product unique.
.
├── package-lock.json ├── package.json └── src ├── app.ts ├── index.ts ├── components │ └── user │ ├── user.controller.ts │ ├── user.entities.ts │ ├── user.module.ts │ ├── user.router.ts │ └── user.service.ts │ ├── contracts │ ├── errors.ts │ ├── mailer.ts │ └── user.js ├── loaders │ └── routes.ts │ ├── utils │ ├── cache.ts │ ├── config.ts │ ├── helpers.ts │ ├── hooks.ts │ ├── loggers.ts │ ├── mailService.ts │ ├── middlewares.ts │ └── sms.ts │ ├── index.ts │ └── server.ts
- Install Docker
- Install Node.js version 16.14 or above (which can be checked by running
node -v
). You can use nvm for managing multiple Node versions installed on a single machine.
You can install dependencies using npm
, pnpm
or yarn
:
Using npm
:
npm install
Using pnpm
:
pnpm install
Using yarn
:
yarn install
For the manual setup clone this repository
The project is configured to use pnpm workspaces, which means that you can install node_modules
of all packages in
repository, with single command:
pnpm install
npm run start
npm run dev
- Node.js
- Express
- Typescript
- Redis
- MongoDB
- JWT
- Bcrypt
Infrastructure:
- Linux Ubuntu 20.04
- Github or Bitbucket
3rd party services:
- Stripe (payments and subscriptions) 'To be implemented'
- Twilio (SMS)
- NodeMailer (Emails)
The application was divided into components. Each component has its own module, where we instantiate the controller, service, and router. This helps us keep our code clean and maintainable.
const userService = new UserService();
const userController = new UserController(userService);
const userRouter = new UserRouter(userController);
you’ll also have an entities file where you'll define the DB entities of your component.
export default mongoose.model < IUser > ("User", UserSchema);
Within the router, you should define the URLs of our backend API and call the corresponding controller functions to handle requests.
class UserRouter {
userController: UserController;
constructor(UserController: UserController) {
this.userController = UserController;
}
getRouter = () => {
const router = Router();
router.post(
"/register",
[checkPhone, checkEmail],
this.userController.register
);
};
// ... rest of the router methods
}
The controller receives incoming requests from the router and prepares the necessary parameters to call the appropriate service functions. Here, we define the logic for handling each API endpoint of our backend.
class UserController {
userService: UserService;
constructor(UserService: UserService) {
this.userService = UserService;
}
register = async (req: Request, res: Response) => {
// ... controller logic
};
// ... rest of the controller methods
}
The service is responsible for handling the business logic of our application. It receives the necessary parameters from the controller, calls the corresponding repository functions, and returns the response to the controller.
class UserService {
addUser = async (user: UserInput<IUser>) => {
try {
const newUSer = new User(user);
return await newUSer.save();
} catch (error) {
throw error;
}
};
// ... rest of the service methods
}
The module is where we instantiate the controller, service, and router. This helps us keep our code clean and maintainable.
const userService = new UserService();
const userController = new UserController(userService);
const userRouter = new UserRouter(userController);
- index.ts
- server.ts
- loaders/routes.ts
MEN Boilerplate is licensed under the MIT License.
I welcome contributions from anyone interested in improving MEN Boilerplate. Please remember that this project follows a Code of Conduct to ensure a welcoming community for all.
For more detailed information on how to contribute to this project, please refer to our Contributing Guide.
If you have any questions about contributing, please contact me on Twitter Me - I would be happy to talk to you!
Thank you for considering contributing to MEN Boilerplate!