Accelerating Server-Side Development with Fastify by Manuel Spigolon & Maksim Sinik & Matteo Collina

Accelerating Server-Side Development with Fastify by Manuel Spigolon & Maksim Sinik & Matteo Collina

Author:Manuel Spigolon & Maksim Sinik & Matteo Collina
Language: eng
Format: epub
Publisher: Packt
Published: 2023-11-15T00:00:00+00:00


Note that we added a name to every handler function for clarity and as a best practice, since it helps to have better stack traces.

Now, let’s look at how to use this plugin module inside our application.

Register routes

Solely declaring the routes plugin doesn’t add any value to our application. Therefore, we need to register it before using it. Thankfully, we already have everything from the previous chapter to auto-register these routes. The following excerpt from apps.js shows the vital part:

// ... fastify.register(AutoLoad, { dir: path.join(__dirname, 'routes'), // [1] indexPattern: /.*routes(\.js|\.cjs)$/i, // [2] ignorePattern: /.*\.js/, autoHooksPattern: /.*hooks(\.js|\.cjs)$/i, autoHooks: true, cascadeHooks: true, options: Object.assign({}, opts) }) // ...

This code snippet uses a plugin called @fastify/autoload to automatically load routes and hooks from a specified directory.

We specified the routes folder ([1]) as the path where our routes are located, and then we defined the regular expression pattern ([2]) to identify the route files. Therefore, to make Fastify pick our previous routes.js file, we must save it in the ./routes/todos/routes.js file.

You may be wondering why we added that todos subfolder to our path. AutoLoad has another neat behavior – it will automatically load all the subfolders of the specified path, using the folder name as the prefix path for the routes we define. Our handlers will be prefixed with the todos path when registered by the Fastify application. This feature helps us organize our code in subfolders without forcing us to define the prefix manually. Let’s make a couple of calls to our application routes to make some concrete examples.

We need two terminals opened, the first to start the application and the second to make our calls using curl.

In the first terminal, go to the project root and type npm to start, as shown here:

$ npm start {"level":30, "time":1679152261083, "pid":92481, "hostname": "dev.local", "msg": "Server listening at http://127.0.0.1:3000"}

Now that the server is running, we can leave the first terminal open and go to the second one. We are ready to make the API calls:

$ curl http://127.0.0.1:3000/todos {"data":[],"totalCount":0}% $ curl http://127.0.0.1:3000/todos/1 {}%

In the preceding snippet, we can see we made two calls. In the first one, we successfully called the listTodo handler, while in the second call, we called readTodo.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
SvelteKit Up and Running by Dylan Hildenbrand(6384)
Building Blazor WebAssembly Applications with gRPC by Václav Pekárek(3718)
Hands-On Application Development with PyCharm by Bruce M. Van Horn II
 Quan Nguyen(2194)
Designing Web APIs with Strapi: Get started with the Strapi headless CMS by building a complete learning management system API by Khalid Elshafie Mozafar Haider(1055)
Django 4 for the Impatient. Learn the core concepts of Python web development with Django in one weekend by G. Lim D. Correa(910)
Vue.js 3 Design Patterns and Best Practices by Pablo David Garaguso(853)
Accelerating Server-Side Development with Fastify by Manuel Spigolon & Maksim Sinik & Matteo Collina(850)
Drupal 10 Module Development - Fourth Edition by Daniel Sipos(766)
Mastering CSS Grid by Thormeier Pascal;(730)
Going the Distance with Babylon.js: Building extensible, maintainable, and attractive browser-based interactive applications using JavaScript by Josh Elster(701)
Simplifying State Management in React Native by Aleksandra Desmurs-Linczewska(619)
Java Memory Management by Maaike van Putten & Seán Kennedy(549)
Hands-On Application Development with Pycharm by II Bruce M. Van Horn;Nguyen Quan;(533)
Joomla!® Explained: Your Step-by-Step Guide (Joanne Romanovich's Library) by Stephen Burge(323)
Python & JavaScript Mastery: 2 Books In 1- Learn And Master Two Powerful Programming Languages by Alex iversion(312)
Beginning Modern JavaScript: A Step-By-Step Gentle Guide to Learn JavaScript for Beginners (Code With Nathan) by Sebhastian Nathan(263)
Understanding JavaScript Promises by Nicholas C. Zakas(251)
Programming With Java by Edet Theophilus(247)
Create GUI Applications with Python & Qt6: The hands-on guide to making apps with Python by Martin Fitzpatrick(246)
NextJS 13 and React Crash Course: Build a Full Stack NextJS 13 App with React, Tailwind and Prisma backend by Lim Greg(236)