A progressive Node.js framework for building efficient and scalable server-side applications.
Nest framework TypeScript starter repository.
$ npm install
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
# Create new project
$ nest new <name>
# Generate a new module
$ nest generate module <name>
# Generate a new controller, [--flat Do not generate a folder for the element].
$ nest generate controller <name> --flat
## Further reading
https://docs.nestjs.com/cli/usages
- Controller - Handles incoming requests
- Services - Handles data access and business logics
- Modules - Groups together code
- Pipes - Validates incoming data
- Filters - Handles error that occur during request handling
- Guards - Handles authentication
- Interceptors - Adds extra logic to incoming requests or outgoing responses
- Repositories - Handles data stored in a DB
- Use global validation pipe [in main.ts]
- Create DTO, a class that tells the different properties of the request body
- Add validation rules to the class [npm class-validator]
- Apply the class to the request handler
Services and Repositories both are classes, however services are the best place to keep the business logics and repositories are the best place to keep storage related logics.
- It states classes should not create instances of its dependencies on its own. It. is a design principle that allows classes to be loosely coupled and, therefore, easier to test and maintain.
- Dependency injection is an inversion of control technique wherein you delegate instantiation of dependencies to the IoC container, instead of doing it in your own code imperatively.
Use the injectable decorator on each class and add them to the modules list of provider
- At startup, register all classes with the container.
- Container will figure out what each dependencies and each class has.
Nest will try to create controller instances
- Then container creates an instance of a class.
- Container builds all the dependencies and provide us the instance.
- Container will reuse the dependencies if needed.
Further reading - https://www.educative.io/edpresso/what-is-inversion-of-control
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Nest is MIT licensed.