Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Solid NodeJS with TypeScript, Jest & NestJS

2,482 views

Published on

A walkthrough the main principles to reach solid NodeJS Applications with TypeScript language, Jest as Test Runner and NestJS as framework for structure.

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Solid NodeJS with TypeScript, Jest & NestJS

  1. 1. Solid NodeJS with TypeScript, Jest & Nest
  2. 2. “We build our computer systems the way we build our cities: over time, without a plan, on top of ruins.” Ellen Ullman
  3. 3. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 3 Flexibility Its multi-paradigm nature, dynamic type system and minimal core allow great flexibility. Ecosystem Great system with largest amount of packages and best evolution. Ubiquity Presence both in client and server, along with mobile environment and almost everywhere. JavaScript
  4. 4. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 4 Modularity Keep simple parts short, connected with clean interfaces. Composition Make those independent packages work together to build applications Simplicity JavaScript succinct syntax and flexibility, along with Node inherent asynchrony NodeJS
  5. 5. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 5 Lack of common architecture Different approaches for directory and file structure, and even module design. Fragile execution Input / Output non-standard type validation, lack of testable parts because of coupling and derived problems from lack of standards. Problematic growth Hard to scale and distribute work among different developers and teams. Medium to Large NodeJS Applications What’s the STRUCTURE? How do I get ROBUSTNESS? What if I need SCALABILITY? Application Issues
  6. 6. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 6 Jest Testing complete solution, with zero config, fast execution, and built-in coverage . Nest Framework based on combination of OOP/FP/FRP, DI and building blocks. TypeScript JavaScript that scales through latest features and solid type static analysis. Weapons Solutions for Robustness, Scalability and Structure
  7. 7. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 7 Why TypeScript? ๏ Types are optional (ideal for validation) ๏ Type inference for static analysis and tooling ๏ Interfaces for solid components ๏ Advanced ESNext features in Development ๏ Compiles to clean, efficient, compatible JavaScript
  8. 8. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 8 Why Jest? ๏ Complete Testing Solution ๏ Minimum configuration ๏ Fast parallelization of tests across workers ๏ Selective execution when watching ๏ Built-in code coverage report with Istanbul ๏ Snapshot Testing included
  9. 9. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 9 Application Architecture Nest provides building blocks around a execution context to give a common architecture, that includes controllers, modules, pipes, guards, etc. Middleware structure Built on top of Express, it leverages the middleware capabilities of this framework.. Dependency Injection All elements in Nest are defined around DI principles, so services, modules, controllers, all can be injected and thus easily testable. Platform-agnostic Reusable pieces that can be used in different contexts, like GraphQL, Microservices or Websockets.. Nest
  10. 10. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 10 • Starter with TypeScript • V5 is coming (beta) with
 dedicated CLI • Basic structure with
 conventions • Main entry point First steps
  11. 11. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 11 ๏ Each Part of the Application ๏ Can be only one: Root Module ๏ Domain Bounded Contexts ๏ Module Decorator describes: ➡ Imports: Other modules ➡ Controllers: Created by the module ➡ Components/Providers: Instantiated and shared
 across the module ๏ Modules are Singletons so they are shared. Modules
  12. 12. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 12 Every Module can Import
 and Export other Modules Admin ModuleUsers Module Application Module Stats ModuleBilling ModuleChat Module Game Module
  13. 13. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 13 Example
  14. 14. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 14 Controllers ๏ Request Handlers and Response Generators ๏ Must be declared associated to a Module ๏ Metadata in decorator defines prefix like @Controller(‘users’) ๏ Two approaches to handle Response: ➡ Nest: Returns Array or Object automatically as JSON ➡ Express: Injected through @Res() decorator.
 Allow express-like response manipulation. ๏ POST handler can use Data Transfer Object (DTO)
  15. 15. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 15 Example
  16. 16. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 16 Components / Providers ๏ Services, Factories, Helpers, etc ๏ They can be Injected into Controllers or
 other Components / Providers through constructor ๏ Any complex task performed into Controllers ๏ Pattern of reusability and modularity ๏ Plain TypeScript Classes
  17. 17. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 17 Example
  18. 18. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 18 Middlewares ๏ Function called before the Route Handler ๏ Have access to Request, Response and Next handler ๏ Same capabilities as express middlewares: ➡ Execute code before continuing: And then call next() ➡ Make changes to Request & Response ➡ End Request-Response cycle ๏ Can be defined: ➡ For specific paths ➡ For specific Controllers
  19. 19. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 19 Example
  20. 20. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 20 Exceptions ๏ Global Exception Filter for Unhandled Errors: 500 Server Error ๏ Built-in HttpException that when thrown by handler
 it transforms to proper JSON response ๏ A good practice is to create your own Exception Hierarchy with
 exceptions inherited by the HttpException class ๏ Nest offers several built-in HTTP Exceptions like:
 BadRequestException, UnauthorizedException, 
 NotFoundException, ForbiddenException, etc
  21. 21. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 21 Example
  22. 22. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 22 Validation ๏ Validation is done through a specific Pipe ๏ Pipes are classes that implement the PipeTransform Interface ๏ A Pipe transforms the input data to desired output before Route Handler ๏ ValidationPipe is a built-in Pipe ๏ Data Transfer Object (DTO) is required to receive the @Body() ๏ Class-Validator library allow decorator-based validation on DTO definition
  23. 23. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 23 Example
  24. 24. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 24 Testing with Jest ๏ Every building block in Nest (Components, Controllers, etc) are simple decorated classes ๏ As every dependency is injected through constructor, they are easy to mock ๏ Recommendation is to keep your test files near the implementation ๏ Recommendation is always isolated tests ๏ Test class is a utility with createTestingModule() that takes module metadata and creates
 a TestingModule instance.
  25. 25. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 25 Example
  26. 26. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 26 Example
  27. 27. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 27 Database Access ๏ Nest uses by default the standard TypeORM for Object Relational Model ๏ Mongoose use the built-in @nestjs/mongoose package
  28. 28. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 28 Example
  29. 29. NODEJS SÓLIDO CON TYPESCRIPT, JEST Y NESTJS. 29 GraphQL Module for GraphQL server with Apollo Websockets Decorators to include WS transport Microservices NestJS microservices are TCP interconnected services Platform-agnosticism allow building apps with several transport layers and contexts.
  30. 30. Always bet on JavaScript. Let’s make JS applications more solid. Let’s do it on scale. “It's never on how difficult is to write bad code, it's on how easy is to write great code.” @yonatam
  31. 31. Thank you No real requests were damaged during this talk.

×