You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal
Change the eject file example to validate everything before running the server and make use of the .ready() function.
Motivation
What made me think of this approach are:
Fastify encapsulation concept, the server should not run if something goes wrong with plugins and are not loaded inside correctly inside Fastify.
The struggle I've been in the past few days trying to use Fastify in a complex real-world project and I didn't find clear examples.
easier to handle errors, if something goes wrong Fastify will throw the valid error not some error buried deep inside the router or hooks
the unclear error "cannot read properties of undefined (reading 'length')" which was happening for several reasons and I cannot detect them until i throw error on startup
Example
// Read the .env file.import*asdotenvfrom"dotenv";dotenv.config();// Require the frameworkimportFastifyfrom"fastify";// Require library to exit fastify process, gracefully (if possible)importcloseWithGracefrom"close-with-grace";// Instantiate Fastify with some configconstapp=Fastify({logger: true,});// make use of the `.ready()` to prevent going on race conditionfastify.register(app).ready((err)=>{// throw error if something happened on registering `plugins` or `routes`if(err)throwerr;// delay is the number of milliseconds for the graceful close to finishconstcloseListeners=closeWithGrace({delay:
parseInt(process.env.FASTIFY_CLOSE_GRACE_DELAYasstring)||500,},asyncfunction({ signal, err, manual }){if(err){fastify.log.error(err);}awaitfastify.close();}ascloseWithGrace.CloseWithGraceAsyncCallback);fastify.addHook("onClose",async(instance,done)=>{closeListeners.uninstall();done();});//server listenconstport=process.env.PORT||8000;// Start listening.fastify.listen({port: parseInt(portasstring)},(err: any)=>{if(err){fastify.log.error(err);process.exit(1);}});});
The text was updated successfully, but these errors were encountered:
I don't think close-with-grace should be within a plugin or a callback, but rather at the top level, installed after the server has been setup.
It's a property of the global application and not of the server, and you can install only one. Therefore, you should put that into the "main" of your application outside of all plugins.
Prerequisites
🚀 Feature Proposal
Change the eject file example to validate everything before running the server and make use of the
.ready()
function.Motivation
What made me think of this approach are:
plugins
and are not loaded inside correctly inside Fastify.Example
The text was updated successfully, but these errors were encountered: