Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NestJS

 NestJS

In recent years, many Javascript projects quickly improve developer productivity at building front applications (Angular, React, Vue, etc. ). But what about back applications in Javascript ? Well, there’s a decent choice of very good NodeJs-based libraries and frameworks such as Express, Hapi, Sails, Meteor, etc. However, most of them miss one important thing: clear architectural guidelines. Nest tries to fill this gap ( and actually does well ) and provides you additional up-to-date and desirable server-side features such as Injection Dependency, Microservices, Websocket, Caching, MVC, GraphQL, CRQS, OpenAPI, etc.

Avatar for kawan16

kawan16

May 07, 2019
Tweet

More Decks by kawan16

Other Decks in Programming

Transcript

  1. hello! I am Karl Devooght You can find me at

    @karl.devooght I’m a frontend freelance developer And a lot of fancy role names ! 2
  2. A (very) brief history of js backend . 4 Node.js

    Express.js Hapi.js 27 May 2009 2010 2013
  3. NEST in a word . 8 An higher level of

    abstraction based on Express or Fastify
  4. NEST in a word . 10 Come with a lot

    of tools, techniques & recipes
  5. Module 14 ✘ Container of cohesive code related to a

    part of the application domain ✘ At least one module => Root Module ✘ Feature Module & Shared Module ✘ Easily turn into micro-service
  6. Controller . 16 ✘ Handling incoming request and returning response

    ✘ Massive use of decorators Routing, Request Params / Body, Headers, etc. ✘ Deal with asynchronicity ✘ One controller for all requests ( by default )
  7. Provider . 17 ✘ Something that can inject dependencies (

    IoC ) ✘ Every Nest entity has a provider ✘ A class with @Injectable ✘ SOLID compliant
  8. Custom Provider . 18 Instance ID / Token String Symbol

    Class ... Instance Maker useClass useValue useFactory
  9. Middleware . 19 ✘ Function called before route handling ✘

    Access to request and response ✘ Same meaning than Express middleware ✘ Call next() when intend to move forward
  10. Guard . 20 ✘ Determine if a request should be

    handled by a route handler ✘ Class with a canActivate method ✘ Access to the execution context ( eg. route handler ) ✘ Can enrich execution context with metadata
  11. exception . 21 ✘ Handle exceptions across the application ✘

    Built-in Http Exception ✘ Exception filter
  12. pipe . 22 ✘ Transforms input data to desired output

    ✘ Can act as data validator using joi or class-validator ✘ Built-in pipes such as ValidatorPipe
  13. database . 24 ✘ Object Relational Mapper ✘ Support of

    a dozen of DB ✘ Repository Design Pattern ✘ Decorators ( @Entity ) TypeORM git push -u origin master
  14. database . 25 ✘ CRUD-based generated ✘ Custom Repository ✘

    Just need to inject Repository git push -u origin master
  15. database . 26 ✘ Multi-database support ✘ Access to connection

    object ✘ Access to entity manager object Misc git push -u origin master
  16. Configuration . 28 Dotenv git push -u origin master ✘

    Loads environment variable from env. file ✘ Use of parser service ✘ Inject anywhere into the app
  17. caching . 29 cache-manager git push -u origin master ✘

    Cache library for Node JS ✘ Unified API for storage providers ✘ Inject anywhere into the controllers git push -u origin master
  18. serialization . 30 class-transformer git push -u origin master ✘

    Data manipulation before sending them ✘ Exclude, Transform & Compute properties ✘ ClassSerializerInterceptor by default
  19. compression . 31 compression git push -u origin master ✘

    JS library for compression ✘ Just an express middleware
  20. Pros 36 ✘ Request what you want no more no

    less ✘ Multiple transports ( HTTP, MQTT, WebSocket ) ✘ “True” Single API for multiple clients ✘ Self-documenting through GraphQL schema
  21. How can we do that in Nest ? How can

    i switch from rest to graphql ? 43
  22. First approach . 44 ✘ Install some dependencies ( Appolo

    Server & @nestjs/graphql ) ✘ Define Graphql schema file ( .graphql file ) ✘ (Option) Generate related typings ( ts-node generate-typings ) ✘ Import and configure a Nest Graph module ✘ Convert controller to graphql resolver
  23. Nice but… I don’t want to learn a new syntax

    I don’t want to generate typings Just keep focus on my TS code 48
  24. second approach . 49 ✘ Install some dependencies ( Appolo

    Server & @nestjs/graphql ) ✘ Import and configure a Nest Graph module ✘ Decorate your DTO types with type-graphql decorators ✘ Convert controller to graphql resolver with a bit of further description
  25. Define a microservice 57 MS MS MS MS Module A

    Module B Module C Module D MODULE E MODULE F