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 [Manuel Spigolon, Maksim Sinik & Collina, Matteo]
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2023-05-15T00:00:00+00:00


These changes are breaking our setup due to the following reasons:

The config.js file is not loaded

The mongo-data-source.js file relies on fastify.secrets

To fix them, we need to edit app.js, as follows:

await fastify.register(require('./configs/config')) fastify.log.info('Config loaded %o', fastify.config)

These lines must be added after the autoload schemas configuration because we validate the environment variable through the schema:dotenv schema.

After that, we can update the plugins’ autoload options as follows:

fastify.register(AutoLoad, { dir: path.join(__dirname, 'plugins'), dirNameRoutePrefix: false, ignorePattern: /.*.no-load\.js/, indexPattern: /^no$/I, options: fastify.config })

Finally, we can fix the mongo-data-source.js file by removing a lot of code:

module.exports = fp(async function (fastify, opts) { fastify.register(fastifyMongo, opts.mongo) })

As you can see, it has become much lighter. We have removed the dependencies parameter as well because we don’t want to access the fastify.secret decorator.

This change has a significant impact on the code logic. With this code restyle, the mongo-data-source.js file is decoupled from the rest of the application because all the settings are provided by the input opts argument. This object is provided by the @fastify/autoload plugin, mapping the options parameter.

You now have a comprehensive and solid knowledge of the configuration and how to best manage it. You can use the previous code example to become confident in tweaking the plugins and playing within the autoload plugin. You will find that the source code in the book’s repository adopts the first solution we saw in the Loading the plugins’ configurations section.

To complete the project scaffolding, we need to add a few more features that are key pieces to consider this basic structure solid and ready to use for our development process. We will learn about some new plugins that add these missing capabilities to our application.



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.