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...