 Hi everyone! I am Matteo Collina and today I'm going to talk to you about Fastify. You know, Fastify is a project that I started a few years back with with Thomas and it has been one of the most amazing, you know, journey of my life, to be honest. So I hope you would like this intro and you would, you know, give it a shot and try it out in your next project. So, let's start. First of all, take a moment to follow me on Twitter at Matteo Collina. I repeat about a lot of things. So, hey, Mollie Javascript. So I also work for a company called NearForm. So if you are interested, please check us out. We are a professional services company based in Ireland but working worldwide to, that can help you in any sort of problems. Anyway, let's get started on Fastify and why it matters. So Fastify is, you can find everything for rated to Fastify on www.fastify.io. It's our main domain. You can find all the docs there. It's pretty good and it's, you know, it should be a lot of docs. As usual, everything can be improved. So please, please, please check it out. And if you see something that it does not, you know, that you don't find clear, please open an issue and maybe send out a PR to improve it. Anyway, and this is actually a good segue for the very first important part of this talk, which is, you know, a genetic principle that is based, that underpins all of OpenJS project, but, you know, specifically Fastify. An open source project is as good as its community is. This, it's extremely important. So in fact, Fastify, we believe that, you know, building and maintaining a web framework for Node.js is too much of a work for a single individual or even a single company to some extent. And we, you know, we encourage everybody, everybody that is using Fastify that is finding values Fastify to contribute back. Also, Fastify was born when I met Thomas and I tried to convince him to help me, to convince somebody helping in his journey. And, you know, I found Thomas and, you know, we started working on this together. So it's quite a nice story. I talk about it in the latest in last year of OpenJS. So check it out. Anyway, Fastify. Fastify is one of the fastest web framework for Node.js. And you can, you can just, you know, require it or import it does work both ways. And you can call it with logger or other options. And then you can declare your routes and, you know, async await, you can use async await to just return an object it will convert to JSON. Very simple. Now, Fastify has been under massive growth in the last year, in the last few years. It has now reached 900,000 downloads per month. And it's probably on the track to reach a million downloads per month at the end of the year. So finger crossed. And it has now, it has 15 collaborators working on it. And it has reached version three. And it has a very big ecosystem of 185 plugins. We are going to ship V4 soon. So we're going to, to storm with a lot of, with a lot of news about it. As I said, the core values as Fastify are all about open collaboration and collaboration across multiple individuals. We always welcome first time contributors. So if you're using Fastify, there's a very good chance that you are, that you will be asked a very, very simple question. Would you like to send up a request to fix your bug, your issue or implement a new feature? It's always important that you contribute back in this ecosystem. We don't, that's what we do here. Okay. It's, it's too complex to build this thing alone. So we ask everybody to contribute back. We also seek open, we also approach with an open governance fashion, which means that we ask if people objects and if not we are lending them. Typically, if there are ties or things that are too complex or that require that the community is on shore, one of the lead maintainers or creators, whatever, step in and try to fix it. Okay. And, or maybe try to break the tie, essentially, and provide some guidance. And this person is Thomas and myself. The fund, as I said, the key part of Fastify was built, Fastify was a community of two people before it was, before any code was committed. So, you know, the fact that it's a meld of different companies, different people, different individuals working together, it's, it's a key, a fundamental issue part of Fastify. It's the problem that we have seen, why is this? Well, the reason is because we have seen that, especially in other frameworks, in other initiatives that it's very hard to, for a single company or person, justify the investment that is needed to build and maintain a web framework for Node.js. And what we have chosen is to, the job that we have chosen is to spread this effort across the companies using it. So, you know, the typical request that I ask everybody, that we ask everybody is, when there is a bug, I said, would you like to send a PR? And remember to add unit tests. So, Fastify is pretty unique in that sense. And we create this culture of ownership that is a key, a fundamental part of the Fastify experience. You can check it out. We also have a nice governance document. So, please check it out to see how our community works. By the way, we just can make, if you contribute to, if you contribute to Fastify, it's very likely that you will get the commit bit. So, please, you know, if you do sustainable and valuable contributions. Let's go into tech, because, you know, still Fastify is a web framework, right, for Node.js. So, it's a tech. We need to talk about tech. The first principle is about overhead. We don't want to add additional overhead in production. Why? That is not truly possible. So, it's aspirational and we want to reduce the overhead that the web framework have. We want to provide a good developer experience. That's a key fundamental part of it. And it needs to work great for both small and big projects. So, it needs to work for a very simple, hello world, but also, you should also work well for a company that is 100 developers working on several microservices and so on and so forth. It's not simple to build something to design a web framework that can easily grow and increase the amount of complexity. Because of this, we wanted to be able to migrate to microservices and serverless and back. It needs to have a very good security and more importantly, data validation. It has a plugin system. Again, we don't believe to scope creep. So, to avoid scope creep, we created a plugin system. So, if something could be a plugin, it likely should. It needs to be testable and we do not want to monkey patch core. To be honest, this was one of the part that, one of the critical part, the node core. So, this one, a critical part that led me to create Fastify in the first place, because a lot of the other frameworks actually had a huge focus on monkey patching node core. Also, we want to follow semantic versioning and long-term support. So, more or less, we ship one new major version per year. And last, but not least, HTTP 1.1 spec adherence. So, we want to follow HTTP 1.1. So, one of the very nice bits, one nice thing to chat about is, how fast is Fastify? Because it's one of, you know, it's cool in this way and it's, I wanted to clarify that Fastify is fast enough. So, it does not provide a significant slowdown compared to node core. In certain cases, actually slightly faster than node core, because it can avoid certain, you know, path that are not great. It's probably faster than whatever you are using. And at this level, it's, you know, comparable to you just to use node core. This is critical because we don't want a significant drop in throughput and performance. So, if there is a significant drop in throughput and performance, we are in, you know, you don't want, your web framework should not be your main bottom, right? It, I'll talk a little bit more about later on why this is fast, why it's fast. So, Fastify is also very much loved by devs. And, you know, it was part of the latest state of JS, which might not be representative, but it's still a good number that, you know, a few people are loving Fastify. So, pretty good, right? I am pretty, pretty happy about about this result. So, it's pretty good. Anyway, Fastify is also have another library called Mercurius that you can use on top. It provides support for GraphQL on top of it. And it's also, it's as well very fast. As I said, one of the key part of Fastify is its plugins. And we have both core plugins and ecosystem plugins. What is the difference? Well, the difference is that core plugins are maintained by the Fastify organization. So, we typically guarantee that they work on every new major. So, we typically update all of them when we do a Sanver major. Now, consider it, we are releasing essentially to do a Sanver major Fastify. We are probably using 4045 modules. So, it's quite a big deal that typically spans across, you know, a couple of days. So, when that's happened, it's a little bit of a shift in major versions. Anyway, they also tend to solve the problem in the most idiomatic way. So, there's a few examples like Fastify to load, Fastify static and the under pressure module. The project in the ecosystem are maintained by members of the community. For example, I wrote some of them that are very opinionated and think that I would not want to include in the org. So, it's, you know, there are plenty. So, hey, and we have a few, we have 141 of those. So, you can just write one, to be honest. There is a good guide on the website. So, what is the target architecture of a Fastify application? Well, historically, you have, we have been deploying Node.js with MVC pattern. MVC, however, as a significant problem. You know, once the complexity of the application grows over time, you are either moving, you're either putting the complexity in the models, the views, or the controllers. So, that's quite of a big, quite of a big problem, for me at least. Because, you know, it's very hard to scale. So, how can we improve this? Well, yeah, here we go. So, how can we improve this? Well, what we can do is we can, you know, start structuring our application instead of using the MVC concept, using just horizontal modules that are, you know, aligned with the features of your app. So, instead of having like these big three boxes where you need to fit everything, you can have just a few independent modules as many as the domain of your application are. And this system actually scale way better in complexity. Because the moment you start doing this, you can, you know, you can have, you have segregated the complexity into and tracking the pieces that you need to reuse in some other module, essentially. So, you can now track what is the interrelationship between the things. In fact, it's also the stepping stone for moving into a microservice, into a microservice architecture. Microservices are actually a fundamental part of how we develop system as well as lambdas and so on. But the key part is that with Fastify, you could essentially move from a monolith to a microservice ward very easily, because it's all built on a console of plugins. So, you will see that in a moment, but it's like super easy. So, let's go a little bit in depth. So, Fastify is built on various levels. So, at the bottom of it, we have two libraries, Avio, which is the loader of Fastify and provide, it also provides the plugin system. And Pino, which is the fastest logger for Node.js. I'm not going to talk about Pino. So, you know, go Google it up, Pino logger Node.js or something. There's a lot of talks. One of my favorite one is the cost of logging. It's been around for a while. It has now reached version six and it's going to have version seven. We're talking a little bit about it later maybe. Anyway, Avio, which is the loader, it's what, you know, load the plugin system. And it provides the extensibility that is needed by your app. Avio guarantees to have the plugin system, the hook system and the decorators. So, it's essentially provide the basics in which all these other features are built upon. On top of that, we have like the router, which is Find My Way, AJV, which is the validation, validator, and Fastify, which is the signalizer. We'll talk about those in a second. So, the plugin system. This is a key part of Fastify. With the plugin system, you have an entry point at the very top. And then the entry point can, you know, load plugins. And plugins can load other plugins re-enter in a re-entrant way. Now, each one of those plugins creates an encapsulation context. So, essentially, whatever they do, it won't influence, it won't influence like sibling plugins, only descendants. This is critical, because then we can say, for example, we can pack an app and reuse it. We'll see how to do that. In fact, if I want to expose a functionality from one plugin to the other, I can use the concept of decoration. So, I can decorate the request, the response, or the server to expose some things to my descendant plugins. Now, this is really nice, because, you know, it's a simple way of providing dependency injection or, I don't know, inversion of control, if you want to call it whatever. So, it's actually pretty nice. Sorry. You can also break the encapsulation if you want, because you can use a utility called Fastify plugin so that you can actually expose the decoration to your parent as well as your siblings. This allows us to build, I don't know, reusable modules. So, we can use this utility to build reusable modules. The request lifecycle of Fastify is different than the one of Express, for example. With other stuff, with other Express or CoA, you have middlewares that are run everywhere. With Fastify, you have a predefined lifecycle that clearly defines that you can clearly use to specify when things are run. And this is key, because in this way, we can actually, you know, avoid monkey patching Nodecore. So, we have all sorts of hooks. So, typically, the most important ones are the on-request hook. That's the first one that is executed. We have prehandler, which is executed before the user handler. And then we have onSend, which is executed before sending the actual response out. We also provide a recommended project structure, which provides an app.js file that contains your app, a plugins folder for reusable behavior, a routes folder for all your routes with a nested pattern. We'll see how to do that, and then a test folder with your code. Fastify provides this concept of encapsulation, as we have seen. Now, with Fastify, you've seen the target architecture on the left. And, you know, it's, let me actually put it, let me actually put it big. Here you go. So, it's, with Fastify, you have, you can see that you have your target architecture on the left. And these architecture provides you, you know, you tell you to structure your system in modules. Now, you can actually port this to Fastify very easily, because you can have, you know, your major application that is composed, your major app that is composed of three subapps or four or five or how many you want, which have their own plugins and dependencies and everything. So, it's completely, they are completely separated between themselves. It's pretty neat. So, because of all this stuff, you can also use Fastify to fully embed and express app. So, you know, it's pretty good. As I said, logging, it's one of the key part of Fastify. And you can just use Pino. So, it's probably the fastest logger for Mochias. So, take a look at the, take a look at Pino. It's great. So, it's, we are versioning on version seven. How do you use it? Well, you just call logger.info. Pretty neat as everything you will use in the past. Some more important bits, Fastify provides out-of-the-box serialization. So, it allows you to specify the type, the schema of the object going out. This is pretty important, because then we can create a faster-than-json.stringify method function that, you know, can give you a nice speed bump. We also written this utility called Fluent JSON Schema to help you write in JSON Schema in a nicer syntax. I like it a lot. I'd never used JSON Schema, by the way. We also use AJV, which is, and by the way, this is the plugin system syntax. So, you need to export a function with app and apps. And AJV is one of the fastest validator for Node.js. So, it's pretty great also for embedding. So, that's why we use it. And hey, it's also another OpenJS project. So, testing. Fastify embed an inject method for fast testing. It's really fast, even this method. And, in fact, that method is so fast that it has the basis of the integration for AWS Lambda. So, you can, with this utility, AWS Lambda Fastify, you can just create a very simple proxy for your Fastify app, which is essentially just the app as you would normally use. And it just works. It's pretty neat. And it's really fast. Like, this is actually way better than embedding and express app inside Lambda. So, you can check it out. You can use the close method for graceful shutdown in normal Node app in case you want to do. Okay. So, I just want to thank you for your time before we go into the, we do a little bit of a live coding example and see how it is. So, I'm going to switch to my terminal that you can all see. So, you know, we can start developing things on Fastify. So, we can do eapp.js and we can do import Fastify from Fastify. And what we do is that we do close app. We do Fastify. Okay. And then we do app.listen, 3000. Here we go. And, you know, because I am fancy, I'm going to bring an await here to demonstrate top level await of Node.js. So, we now do, we can do node app. And then I can call it. And we are calling it. And you see, it's just not found. So, hey, not super nice, right? But we can add it out. And we add it out. And function. I don't need anything here. So, I'm just going to return hello. We're running my app. As you see now, it's returning a lowered. Cool. Oh, I can also see that if there are old HTTP headers that I would expect and they are all there. Okay. So, pretty nice. Okay. What do we do next? Well, this app is not really, really reusable. So, what we want to do is we want to make, well, this is fresh. Okay. Let's test this. Okay. So, we want to create another server, another file called server.js. Now, I want to split my app. Because, for example, I want to build them and we're using for unit tests. So, what I do in app is that I do export default async function. And I typically do this return. Then I move this into server. And here, I do import build from .js. And then I do const app await build. Okay. So, let's see if, node app, it does nothing now. So, if I do node server instead, and then I curl, it's still working as expected. Cool. Okay. Pretty neat. So, this is actually pretty nice. I can also use, for example, passing some ops here and, you know, include them here. And then in server, I can say, for example, logger true. So, I can do node server and then I can curl. Oh, I didn't do something here. So, we'll, did I say this? I probably didn't. Okay. So, now, you see it's printed something and we see some stuff being printed out. This is new light and limited JSON. If I am in, if I want some nicer output for development, I can use Pino Pretty. And as you can see, I can see the request coming in and the request that is completed out. And we can see some usual things about response time and so on and so forth. So, here we go. Pretty neat. Cool. Okay. So, this is some good example. However, this is not really scalable as we saw. So, we can use the routes. We can create a routes folder. And in routes, I can create a load.js. In a load.js, what I am doing is that I'm going to copy this. And here I'm doing to export default async function, app, ops. And then I'm going to pass this in. And then in my app.js, I am just going to app.register. Oh, I need a tiny utility because otherwise it won't work on Windows. So, this is for dealing with DSM. So, it's called DSM. And I'm just going to join import. Well, I want to import join import meta URL, the current file. Well, I don't need this right now. So, I can just do routes and hello.js. Okay. Then I cancel this and see if it works. Okay. So, it did load. And then it's still working. Cool. Okay. So, now one of the things that I want to use is instead, I don't want to load all the files manually. So, what I'm going to do is I'm going to use autoload, the autoload module. So, called fastify autoload. And I'm going to use this with this utility called DSM. And the reason for this it's because unfortunately, you know, in DSM, you don't have an discord.score dename or discord.score file name. So, you need a little bit, you need to work with file URLs and it's a bit convoluted. So, I don't really, I make mistakes all the time, especially stuff that doesn't work on Windows. So, I tend to prefer to use my utility that has this thing called that. So, you do, you specify your directory. And then you say import meta URL, which is the current file. And you put, and then you say routes. So, you basically you tell load, load everything that is defined into that folder. So, you would then know run this and then I can curl it. As you can see, it's still work as expected as before. Pretty nice, pretty, pretty nice indeed. Cool folks. Oh, one of the nicest things that I can actually create a folder nested structure. So, I can say nested and then say route.js. And then I can copy this and put it in here and says alone nested. Note that I still use slash here to define this, that the pattern is defined in the folder. And so, let's see if I can launch this stuff. So, this is still working. Okay, cool. And then if I do slash nested, it's still, it goes into the nested folder. So, yeah, I just wanted to say to conclude this by saying thank you. And if you have any question, please reach out to me at Matteo Colina on Twitter or via email. There is also an at Fastify.js account. So, thank you. Thank you very much. And bye bye.