Avoid autoregistiration of event handler in server folders
I'm using a nuxt 3 application with nitrojs server. Is is possible to define files inside /middleware or /api folders which are not automatically registered and expecting an event handler?
In my usecase I'm trying to split the fetching and mapping logic into different files.
AI says it should be placed in other directories like
/utils
which is working. However, than i have to maintain the same context in two different subpackages. Could i also get this behaviour using the same packages?2 Replies
Hi @Theiaz. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In Nuxt 3, files placed inside the special server directories—such as
You cannot prevent auto-registration for files inside
server/api/
, server/middleware/
, and server/routes/
—are automatically registered as handlers or middleware by Nuxt. This means that any file you put directly in these folders (or their subfolders, if named index
) and that exports an event handler will be auto-registered and exposed as an endpoint or middleware.
To avoid auto-registration of utility or private files:
- Do not place non-handler files directly inside these special directories. Only files meant to be registered as handlers should be placed there.
- For private logic, helpers, or mapping functions, create a separate directory such as server/utils/
, server/lib/
, or any custom folder outside of api/
, middleware/
, or routes/
. Files in these directories are not auto-registered and can be imported manually where needed.
Example structure:
Then, in your redirect.ts
handler, you can import your private logic:
This approach keeps your private logic out of the auto-registered directories and avoids accidental exposure or registration as handlers.
Summary:You cannot prevent auto-registration for files inside
server/api/
, server/middleware/
, or server/routes/
if they export an event handler. Place private or utility files in a different directory (e.g., server/utils/
) and import them as needed in your handlers. This is the recommended and documented approach in Nuxt 3 for organizing server-side code and utilities Docs: server directory structure.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@518773625537560576> Kapa.ai is still learning and improving, please let me know how I did by reacting below.