From e13121edb30db46cbcefa5966f981f1433693c60 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 6 Aug 2023 22:40:29 +0200 Subject: [PATCH] merge to routing --- docs/content/1.guide/3.routing.md | 74 ++++++++++++++++++ docs/content/1.guide/4.middleware.md | 75 ------------------- .../1.guide/{5.storage.md => 4.storage.md} | 0 .../1.guide/{6.cache.md => 5.cache.md} | 0 .../1.guide/{7.assets.md => 6.assets.md} | 0 .../1.guide/{8.plugins.md => 7.plugins.md} | 0 .../{9.typescript.md => 8.typescript.md} | 0 7 files changed, 74 insertions(+), 75 deletions(-) delete mode 100644 docs/content/1.guide/4.middleware.md rename docs/content/1.guide/{5.storage.md => 4.storage.md} (100%) rename docs/content/1.guide/{6.cache.md => 5.cache.md} (100%) rename docs/content/1.guide/{7.assets.md => 6.assets.md} (100%) rename docs/content/1.guide/{8.plugins.md => 7.plugins.md} (100%) rename docs/content/1.guide/{9.typescript.md => 8.typescript.md} (100%) diff --git a/docs/content/1.guide/3.routing.md b/docs/content/1.guide/3.routing.md index 4871c1ac41..80d889d713 100644 --- a/docs/content/1.guide/3.routing.md +++ b/docs/content/1.guide/3.routing.md @@ -149,3 +149,77 @@ export default defineNuxtConfig({ }) ``` :: + + + +## Route Middleware + +Nitro route middleware can hook into the request lifecycle. + +::alert{type=primary} +A middleware can modify the request before it is processed, not after. +:: + +Middleware are auto-registered within the `middleware/` directory. + +```md +routes/ + hello.ts +middleware/ + auth.ts + logger.ts + ... +nitro.config.ts +``` + +### Simple Middleware + +Middleware are defined exactly like route handlers with the only exception that they should not return anything. + +```ts [middleware/auth.ts] +export default defineEventHandler((event) => { + // Extends or modify the event + event.context.user = { name: 'Nitro' } +}) +``` + +::alert +Returning anything from a middleware will close the request and should be avoided! +:: + +### Execution Order + +Middleware are executed in directory listing order. + +```md +middleware/ + auth.ts <-- First + logger.ts <-- Second + ... <-- Third +``` + +Prefix middleware with a number to control their execution order. + +```md +middleware/ + 1.logger.ts <-- First + 2.auth.ts <-- Second + 3.... <-- Third +``` + +### Request Filtering + +Middleware are executed on every request. + +Apply custom logic to scope them to specific conditions. + +For example, you can use the URL to apply a middleware to a specific route: + +```ts [middleware/auth.ts] +export default defineEventHandler((event) => { + // Will only execute for /auth route + if (getRequestURL(event).startsWith('/auth')) { + event.context.user = { name: 'Nitro' } + } +}) +``` diff --git a/docs/content/1.guide/4.middleware.md b/docs/content/1.guide/4.middleware.md deleted file mode 100644 index 92da08ffee..0000000000 --- a/docs/content/1.guide/4.middleware.md +++ /dev/null @@ -1,75 +0,0 @@ ---- -icon: ri:shield-user-line ---- - -# Middleware - -Nitro middleware can hook into the request lifecycle. - -::alert{type=primary} -A middleware can modify the request before it is processed, not after. -:: - -Middleware are auto-registered within the `middleware/` directory. - -```md -routes/ - hello.ts -middleware/ - auth.ts - logger.ts - ... -nitro.config.ts -``` - -## Simple Middleware - -Middleware are defined exactly like route handlers with the only exception that they should not return anything. - -```ts [middleware/auth.ts] -export default defineEventHandler((event) => { - // Extends or modify the event - event.context.user = { name: 'Nitro' } -}) -``` - -::alert -Returning anything from a middleware will close the request and should be avoided! -:: - -## Execution Order - -Middleware are executed in directory listing order. - -```md -middleware/ - auth.ts <-- First - logger.ts <-- Second - ... <-- Third -``` - -Prefix middleware with a number to control their execution order. - -```md -middleware/ - 1.logger.ts <-- First - 2.auth.ts <-- Second - 3.... <-- Third -``` - -## Request Filtering - -Middleware are executed on every request. - -Apply custom logic to scope them to specific conditions. - -For example, you can use the URL to apply a middleware to a specific route: - -```ts [middleware/auth.ts] -export default defineEventHandler((event) => { - // Will only execute for /auth route - if (getRequestURL(event).startsWith('/auth')) { - event.context.user = { name: 'Nitro' } - } -}) -``` diff --git a/docs/content/1.guide/5.storage.md b/docs/content/1.guide/4.storage.md similarity index 100% rename from docs/content/1.guide/5.storage.md rename to docs/content/1.guide/4.storage.md diff --git a/docs/content/1.guide/6.cache.md b/docs/content/1.guide/5.cache.md similarity index 100% rename from docs/content/1.guide/6.cache.md rename to docs/content/1.guide/5.cache.md diff --git a/docs/content/1.guide/7.assets.md b/docs/content/1.guide/6.assets.md similarity index 100% rename from docs/content/1.guide/7.assets.md rename to docs/content/1.guide/6.assets.md diff --git a/docs/content/1.guide/8.plugins.md b/docs/content/1.guide/7.plugins.md similarity index 100% rename from docs/content/1.guide/8.plugins.md rename to docs/content/1.guide/7.plugins.md diff --git a/docs/content/1.guide/9.typescript.md b/docs/content/1.guide/8.typescript.md similarity index 100% rename from docs/content/1.guide/9.typescript.md rename to docs/content/1.guide/8.typescript.md