A lightweight Node.js application with a custom Dependency Injection (DI) framework, route decorators, and Swagger/OpenAPI integration for API documentation.
- Custom Dependency Injection: A simple DI container for managing dependencies.
- Route Decorators: Use
@Controller,@Get, and@Postfor defining routes. - Validation: Request validation with
@Validateusingclass-validator. - Swagger/OpenAPI Integration: Automatic API documentation generation based on JSDoc and metadata.
- Modular Architecture: Clear separation of concerns between controllers, services, and utilities.
- Node.js: v18 or later
-
Clone the repository:
git clone https://github.com/nuecms/di.git cd di -
Install dependencies:
pnpm install
-
Start the development server:
pnpm run dev
-
Access the API:
- API Base URL:
http://localhost:3000 - Swagger Docs:
http://localhost:3000/api-docs
- API Base URL:
To build and run the project in production:
npm run build
npm startIn nuxt.config.ts add the following code:
to in nuxt project, you need to install vite-ts-decorators.
hooks: {
// https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/core/nitro.ts#L407
'nitro:init': (nitro: Nitro) => {
nitro.hooks.hook('rollup:before', (nitro, rollupConfig) => {
if (Array.isArray(rollupConfig.plugins)) {
const index = rollupConfig.plugins.findIndex((plugin) => plugin && 'name' in plugin && plugin.name === 'esbuild');
rollupConfig.plugins.splice(index, 0, {
...viteDecorators({
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
force: true,
srcDir: 'server/**/*.ts', // Optional: Glob pattern for finding source files
}),
enforce: null,
});
}
})
},
'nitro:build:before': (nitro: Nitro) => {
nitro.options.moduleSideEffects.push('reflect-metadata')
}
},- GET
/users - Response:
[]
- POST
/users - Request Body:
{ "name": "John Doe", "email": "[email protected]" } - Response:
{ "name": "John Doe", "email": "[email protected]" }
Request body validation is powered by class-validator:
- Add a DTO class (e.g.,
CreateUserDto) in thecontrollers/dtosdirectory. - Use
@Validateto enforce validation rules.
Swagger UI is automatically generated:
- Annotate methods with JSDoc comments.
- Use decorators to define routes and validation.
- Accessible at
/api-docs.
- Node.js: JavaScript runtime.
- Express: Web framework for Node.js.
- TypeScript: Static typing for JavaScript.
- class-validator: Validation for DTOs.
- swagger-ui-express: API documentation.
- Add support for custom middlewares.
- Implement dynamic route loading.
- Extend DI with lifecycle management.
- Introduce unit and integration tests.
This project was bootstrapped with Node.js Decorators.
This project is licensed under the MIT License. See the LICENSE file for details.