Skip to content

Messaging app with NestJS, learning the nestJS basics.

Notifications You must be signed in to change notification settings

nikhilbuilds/nestjs-basic

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest framework TypeScript starter repository.

Installation

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Some common commands

# 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

Parts of Nest

  • 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

Setting up automatic validation

  • 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

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.

Inversion of control principle

  • 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.

DI fundamentals

  • 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.

DI Container Flow

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

Support

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.

License

Nest is MIT licensed.

About

Messaging app with NestJS, learning the nestJS basics.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published