Splitting the code into small plugins
We implemented the API root endpoint in the Creating an API starter using Fastify recipe, which is often used as a health check to verify whether the server started successfully. However, we can’t keep adding all the application’s routes to the index.js file; otherwise, it would become unreadable in no time. So, let’s split our index.js file.
How to do it…
To split our index.js file, follow these steps:
- Create an
app.jsfile and move theserverOptionsconstant with the following server configuration:const serverOptions = { logger: true }; - We define our first plugin interface:
async function appPlugin (app, opts) { app.get('/', async function homeHandler () { return { api: 'fastify-restaurant-api', version: 1 }; }); }A plugin is...