Accelerating Server-Side Development with Fastify: A comprehensive guide to API development
9781800563582
Learn to build faster web applications by implementing maintainable and pluggable APIs with Fastify
Key Features Writte
2,337
36
12MB
English
Pages 521
Year 2023
Report DMCA / Copyright
DOWNLOAD EPUB FILE
Table of contents :
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