Accelerating Server-Side Development with Fastify

Book description

Learn to build faster web applications by implementing maintainable and pluggable APIs with Fastify Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Written by Fastify's core contributors to help you adopt the Fastify mindset for API development
  • Gain an architectural overview of Fastify’s microservices development capabilities and features
  • Build complete apps in Fastify, from application design to production

Book Description

This book is a complete guide to server-side app development in Fastify, written by the core contributors of this highly performant plugin-based web framework. Throughout the book, you’ll discover how it fosters code reuse, thereby improving your time to market.

Starting with an introduction to Fastify’s fundamental concepts, this guide will lead you through the development of a real-world project while providing in-depth explanations of advanced topics to prepare you to build highly maintainable and scalable backend applications. The book offers comprehensive guidance on how to design, develop, and deploy RESTful applications, including detailed instructions for building reusable components that can be leveraged across multiple projects. The book presents guidelines for creating efficient, reliable, and easy-to-maintain real-world applications. It also offers practical advice on best practices, design patterns, and how to avoid common pitfalls encountered by developers while building backend applications.

By following these guidelines and recommendations, you’ll be able to confidently design, implement, deploy, and maintain an application written in Fastify, and develop plugins and APIs to contribute to the Fastify and open source communities.

What you will learn

  • Explore the encapsulation techniques implemented by Fastify
  • Understand how to deploy, monitor, and handle errors in a running Fastify instance
  • Organize the project structure and implement a microservices architecture
  • Explore Fastify’s core features such as code reuse, runtime speed, and much more
  • Discover best practices for implementing Fastify in real-world RESTful apps
  • Understand advanced backend development concepts such as performance monitoring and logging

Who this book is for

This book is for mid to expert-level backend web developers who have already used other backend web frameworks and are familiar with HTTP protocol and its peculiarities. Developers looking to migrate to Fastify, evaluate its suitability for their next project, avoid architecture pitfalls, and build highly responsive and maintainable API servers will also find this book useful. The book assumes knowledge of JavaScript programming, Node.js, and backend development.

Table of contents

  1. Accelerating Server-Side Development with Fastify
  2. The Story of Fastify
  3. Contributors
  4. About the authors
  5. About the reviewers
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Download a free PDF copy of this book
  7. Part 1:Fastify Basics
  8. Chapter 1: What Is Fastify?
    1. Technical requirements
    2. What is Fastify?
      1. Fastify’s components
    3. Starting your server
      1. Lifecycle and hooks overview
      2. The root application instance
    4. Adding basic routes
      1. Shorthand declaration
      2. The handler
      3. The Reply component
      4. The first POST route
      5. The Request component
      6. Parametric routes
    5. Adding a basic plugin instance
    6. Understanding configuration types
    7. Shutting down the application
    8. Summary
  9. Chapter 2: The Plugin System and the Boot Process
    1. Technical requirements
    2. What is a plugin?
      1. Creating our first plugin
      2. The alternative plugin function signature
    3. Exploring the options parameter
      1. The options parameter type
      2. The prefix option
    4. Understanding encapsulation
      1. Handling the context
      2. fastify-plugin
    5. Exploring the boot sequence
      1. The register instance method
      2. The after instance method
      3. A declaration order
    6. Handling boot and plugin errors
      1. ERR_AVVIO_PLUGIN_TIMEOUT
      2. Recovery from a boot error
    7. Summary
  10. Chapter 3: Working with Routes
    1. Technical requirements
    2. Declaring API endpoints and managing the errors
      1. Declaration variants
      2. The route options
      3. Bulk routes loading
      4. Synchronous and asynchronous handlers
      5. Reply is a Promise
      6. How to reply with errors
    3. Routing to the endpoint
      1. The 404 handler
      2. Router application tuning
      3. Registering the same URLs
    4. Reading the client’s input
      1. The path parameters
      2. The query string
      3. The headers
      4. The body
    5. Managing the route’s scope
      1. The route server instance
      2. Printing the routes tree
    6. Adding new behaviors to routes
      1. Accessing the route’s configuration
      2. AOP
    7. Summary
  11. Chapter 4: Exploring Hooks
    1. Technical requirements
    2. What is a lifecycle?
    3. Declaring hooks
    4. Understanding the application lifecycle
      1. The onRoute hook
      2. The onRegister hook
      3. The onReady hook
      4. The onClose hook
    5. Understanding the request and reply lifecycle
      1. Handling errors inside hooks
      2. The onRequest hook
      3. The preParsing hook
      4. The preValidation hook
      5. The preHandler hook
      6. The preSerialization hook
      7. The onSend hook
      8. The onResponse hook
      9. The onError hook
      10. The onTimeout hook
      11. Replying from a hook
      12. Route-level hooks
    6. Summary
  12. Chapter 5: Exploring Validation and Serialization
    1. Technical requirements
    2. Understanding validation and serialization
      1. The JSON Schema specification
      2. Compiling a JSON Schema
      3. Fastify’s compilers
    3. Understanding the validation process
      1. The validator compiler
      2. Validation execution
    4. Customizing the validator compiler
      1. Flow control
      2. Understanding the Ajv configuration
    5. Managing the validator compiler
      1. Configuring the default Ajv validator compiler
      2. The validation error
      3. Reusing JSON schemas
      4. Building a new validator compiler
      5. Customizing the schema validator compiler
    6. Understanding the serialization process
      1. The reply serializer
      2. The serializer compiler
      3. Managing the serializer compiler
    7. Summary
  13. Part 2:Build a Real-World Project
  14. Chapter 6: Project Structure and Configuration Management
    1. Technical requirements
    2. Designing the application structure
      1. Setting up the repository
      2. Understanding the application structure
    3. Improving the application structure
      1. Starting an optimal project
      2. Managing project directories
      3. Loading the configuration
    4. Debugging your application
    5. Sharing the application configuration across plugins
    6. Using Fastify’s plugins
      1. How to get a project overview
      2. How to be reachable
    7. Summary
  15. Chapter 7: Building a RESTful API
    1. Technical requirements
    2. Application outline
      1. Defining routes
      2. Register routes
      3. Data source and model
    3. Implementing the routes
      1. createTodo
      2. listTodo
      3. readTodo
      4. updateTodo
      5. deleteTodo
      6. changeStatus
    4. Securing the endpoints
    5. Loading route schemas
      1. Schemas loader
      2. Adding the Autohooks plugin
      3. Implementing the Autohook plugin
      4. Using the schemas
    6. Don’t repeat yourself
    7. Summary
  16. Chapter 8: Authentication, Authorization, and File Handling
    1. Technical requirements
    2. Authentication and authorization flow
    3. Building the authentication layer
      1. Authentication plugin
      2. Authentication routes
      3. Logout route
    4. Adding the authorization layer
      1. Adding the authentication layer
      2. Updating the to-do data source
    5. Managing uploads and downloads
    6. Summary
  17. Chapter 9: Application Testing
    1. Technical requirements
    2. Writing good tests
      1. What tests need to be written?
      2. How to write tests?
    3. Testing the Fastify application
      1. Installing the test framework
      2. Creating your node-tap cheatsheet test
      3. How to write tests with Fastify?
      4. How to improve the developer experience?
    4. Dealing with complex tests
      1. Reusing multiple requests
      2. Mocking the data
    5. Speeding up the test suite
      1. Running tests in parallel
      2. How to manage shared resources?
    6. Where tests should run
    7. Summary
  18. Chapter 10: Deployment and Process Monitoring for a Healthy Application
    1. Technical requirements
    2. Testing our Docker image with a local deployment
    3. Hosting our DB on MongoDB Atlas
    4. Choosing a cloud provider
    5. Deploying to Fly.io
    6. Setting up continuous deployment
    7. Collecting application process data
      1. Collecting the application process data with Prometheus
      2. Exploring the metrics with Grafana
    8. Summary
  19. Chapter 11: Meaningful Application Logging
    1. Technical requirements
    2. How to use Fastify’s logger
      1. How Pino logs the application’s messages
      2. Customizing logging configuration
    3. Enhancing the default logger configuration
      1. How to hide sensitive data
    4. Collecting the logs
      1. How to consolidate the logs
      2. Consolidating logs by using Pino transports
    5. Managing distributed logs
    6. Summary
  20. Part 3:Advanced Topics
  21. Chapter 12: From a Monolith to Microservices
    1. Technical requirements
    2. Implementing API versioning
      1. Version constraints
      2. URL prefixes
      3. Filesystem-based routing prefixes
    3. Splitting the monolith
      1. Creating our v2 service
      2. Building the v1 service on top of v2
    4. Exposing our microservice via an API gateway
      1. docker-compose to emulate a production environment
      2. Nginx as an API gateway
      3. @fastify/http-proxy as an API gateway
    5. Implementing distributed logging
    6. Summary
  22. Chapter 13: Performance Assessment and Improvement
    1. Technical requirements
    2. Why measure performance?
    3. How to measure an application’s performance?
      1. Measuring the HTTP response time
      2. Instrumenting the Fastify application
      3. Visualizing the tracing data
    4. How to analyze the data
      1. Creating a flame graph
      2. How to check memory issues
      3. How to identify I/O resources
    5. How to optimize the application
      1. Node.js internals that you need to know
      2. The safety of parallel running
      3. Connection management takeaways
    6. Summary
  23. Chapter 14: Developing a GraphQL API
    1. Technical requirements
    2. What is GraphQL?
      1. How does Fastify support GraphQL?
    3. Writing the GQL schema
      1. Defining GQL operations
    4. How to make live a GQL schema?
      1. Starting the GQL server
      2. Implementing our first GQL query resolver
      3. Implementing type object resolvers
    5. How to improve resolver performance?
    6. Managing GQL errors
    7. Summary
  24. Chapter 15: Type-Safe Fastify
    1. Technical requirements
    2. Creating the project
      1. Adding the tsconfig.json file
      2. Adding the application’s entry point
    3. Using Fastify type-providers
    4. Generating the API documentation
    5. Summary
  25. Index
    1. Why subscribe?
  26. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Download a free PDF copy of this book

Product information

  • Title: Accelerating Server-Side Development with Fastify
  • Author(s): Manuel Spigolon, Maksim Sinik, Matteo Collina
  • Release date: June 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781800563582