O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Nest.js: A Progressive Node.js Framework

Book Description

JavaScript frameworks go in and out of style very quickly as web technologies change and grow. Nest.js is a good starting point for many developers that are looking to use a modern web framework because it uses a language that is very similar to that of the most used language on the web to this day, JavaScript. Nest.js also uses TypeScript, which is a language that provides the simplicity and power of JavaScript with the type safety of other languages you may be used to. The type safety in Nest.js is only available at compile time, because the Nest.js server is compiled to a Node.js Express server that runs JavaScript. This is still a major advantage, however, since it allows you to better design programs error free prior to runtime.

With Nest.js making use of Node.js Express, you have access to each and every one of the Node.js packages when developing Nest applications. Many even have type definitions for their packages that allow IDE’s to read the package and make suggestions/auto fill in code that may not be possible when crossing JavaScript code with TypeScript code. One of the largest benefits of Node.js is the huge repository of modules that are available to pull from instead of having to write your own. Nest.js includes some of these modules already as part of the Nest platform, like @nestjs/mongoose, which uses the NPM library mongoose.

Angular was a heavy inspiration for the development of Nest.js because both use a Module/Component system that allows for reusability. If you are not familiar with Angular, it is a TypeScript-based front-end framework that can be used cross-platform to develop responsive web apps and native apps, and it functions a lot like Nest does. The two also pair very well together with Nest providing the ability to run a Universal server to serve pre-rendered Angular web pages to speed up website delivering times using Server-Side Rendering (SSR) mentioned above. Follow along in this how-to and get up and running with Nest.js.

Table of Contents

  1. Preface
    1. What is Nest.js?
  2. 1. Introduction
    1. Topics discussed
    2. Nest CLI
    3. Dependency Injection
    4. Authentication
    5. ORM
    6. REST API
    7. WebSockets
    8. Microservices
    9. GraphQL
    10. Routing
    11. Nest specific tools
    12. OpenAPI (Swagger)
    13. Command Query Responsibility Segregation (CQRS)
    14. Testing
    15. Server-side rendering with Angular Universal
    16. Summary
  3. 2. Overview
    1. Controllers
    2. Providers
    3. Modules
    4. Bootstrapping
    5. Middleware
    6. Guards
    7. Summary
  4. 3. Nest.js authentication
    1. Passport
    2. Manual implementation
      1. Implementation
      2. Authentication middleware
      3. Managing restrictions with guards
    3. Nest.js passport package
    4. Summary
  5. 4. Dependency Injection system of Nest.js
    1. Overview of Dependency Injection
      1. Why use Dependency Injection
      2. How it works without Dependency Injection
      3. How it works with a manual Dependency Injection
      4. Dependency Injection pattern today
    2. Nest.js Dependency Injection
    3. The difference between Nest.js and Angular DI
    4. Summary
  6. 5. TypeORM
    1. What database to use
      1. About MariaDB
    2. Getting started
      1. Start the database
      2. Connect to the database
      3. Initialize TypeORM
    3. Modelling our data
      1. Our first entity
    4. Using our models
      1. The service
      2. The controller
      3. Building a new module
    5. Improving our models
      1. Auto-generated IDs
      2. When was the entry created?
      3. Column types
      4. NoSQL in SQL
    6. Relationships between data models
      1. How to store related entities
      2. Retrieving related entities in bulk
      3. Lazy relationships
    7. Other kinds of relationships
      1. One-to-one
      2. Many-to-many
    8. Advanced TypeORM
      1. Security first
      2. Other listeners
      3. Composing and extending entities
      4. Caching
      5. Building a query
      6. Building our model from a existing database
    9. Summary
  7. 6. Sequelize
    1. Configure Sequelize
    2. Create a model
      1. @Table
      2. @column
      3. Create the User model
      4. LifeCycle hooks
    3. Injecting a model into a service
    4. Usage of Sequelize transaction
    5. Migration
      1. Configuring the migration script
      2. Create a migration
    6. Summary
  8. 7. Mongoose
    1. A word about MongoDB
    2. A word about Mongoose
      1. Mongoose and Nest.js
    3. Getting started
      1. Set up the database
      2. Start the containers
      3. Connect to the database
    4. Modelling our data
      1. Our first schema
    5. Using the schema
      1. The interface
      2. The service
      3. The controller
    6. The first requests
    7. Relationships
      1. Modelling relationships
      2. Saving relationships
      3. Reading relationships
    8. Summary
  9. 8. Web sockets
    1. WebSocketGateway
    2. Gateways
    3. Authentication
    4. Adapter
    5. Client side
    6. Summary
  10. 9. Microservices
    1. Server bootstrap
    2. Configuration
    3. First microservice handler
    4. Sending data
    5. Exception filters
    6. Pipes
    7. Guards
    8. Interceptors
    9. Built-in transports
      1. Redis
      2. MQTT
      3. NATS
      4. gRPC
    10. Custom transport
    11. Hybrid application
    12. Advanced architecture design
    13. Summary
  11. 10. Routing and request handling in Nest.js
    1. Request handlers
    2. Generating responses
      1. Standard approach
      2. Express approach
    3. Route parameters
    4. Request body
    5. Request object
    6. Asynchronous handlers
      1. Async/await
      2. Promise
      3. Observables
    7. Error responses
      1. HttpException
      2. Unrecognized exceptions
    8. Summary
  12. 11. OpenAPI (Swagger) Specification
    1. Document Settings
      1. Documenting authentication
    2. Swagger UI
    3. API input decorators
      1. @Body
      2. @Param
      3. @Query
      4. @Headers
      5. Authentication
    4. API request and response decorators
    5. API metadata decorators
    6. Saving the swagger document
    7. Summary
  13. 12. Command Query Responsibility Separation (CQRS)
    1. Entry module commands
      1. Command handlers
      2. Invoking command handlers
    2. Linking keywords with events
      1. Keyword events
      2. Invoking event handlers
    3. Retrieving keywords APIs
    4. Linking keywords with sagas
      1. Keyword saga commands
      2. Keyword saga
    5. Summary
  14. 13. Architecture
    1. Style guide of naming conventions
      1. Controller
      2. Service
      3. Module
      4. Middleware
      5. Exception filter
      6. Pipe
      7. Guard
      8. Interceptor
      9. Custom decorator
      10. Gateway
      11. Adapter
      12. Unit test
      13. E2E test
    2. Directory structure
      1. Server architecture
      2. Angular Universal architecture
    3. Summary
  15. 14. Testing
    1. Unit testing
      1. Tooling
      2. Preparation
      3. Writing our first test
      4. Testing for equality
      5. Covering our code in tests
    2. E2E testing
      1. Preparation
      2. Writing end-to-end tests
    3. Summary
  16. 15. Server-side Rendering with Angular Universal
    1. Serving the Angular Universal App with Nest.js
    2. Building and running the Universal App
    3. Summary