new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Nuxt · API · all subjects

utils/route-middleware

5 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

defineNuxtRouteMiddleware signature

defineNuxtRouteMiddleware is a helper function that creates named route middleware. It takes a RouteMiddleware function as a parameter and returns a RouteMiddleware function. The function signature is: export function defineNuxtRouteMiddleware (middleware: RouteMiddleware): RouteMiddleware

RouteMiddleware interface

The RouteMiddleware interface defines a function that takes two parameters: to (RouteLocationNormalized) as the next route, and from (RouteLocationNormalized) as the current route. It returns ReturnType<NavigationGuard>. The interface is: interface RouteMiddleware { (to: RouteLocationNormalized, from: RouteLocationNormalized): ReturnType<NavigationGuard> }

Route middleware storage location

Route middleware are stored in the app/middleware/ directory of your Nuxt application by default, unless configured otherwise through the Nuxt config.

defineNuxtRouteMiddleware error throwing example

This example shows how to use route middleware to throw errors and display helpful error messages. The middleware checks if the route parameter id equals '1' and throws a 404 error: export default defineNuxtRouteMiddleware((to) => { if (to.params.id === '1') { throw createError({ status: 404, statusText: 'Page Not Found' }) } }) This redirects the user to the custom error page defined in ~/error.vue and exposes the error message and code.

defineNuxtRouteMiddleware authentication redirection example

This example demonstrates using route middleware to redirect users based on authentication status: export default defineNuxtRouteMiddleware((to, from) => { const auth = useState('auth') if (!auth.value.isAuthenticated) { return navigateTo('/login') } if (to.path !== '/dashboard') { return navigateTo('/dashboard') } }) The middleware uses useState to access authentication state and navigateTo to redirect users. Both navigateTo and abortNavigation are globally available helper functions within defineNuxtRouteMiddleware.

Give your agent this brain