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