Search⌘ K

Server Middleware

Explore how to build and implement server middleware in Nuxt 3. Understand its purpose in logging, modifying request context, and how it differs from router middleware to enhance server-side functionality.

On the front end of our project, we could run code that we may want to execute before navigating to a particular route. This was an additional step between navigation, used to perform a task such as checking if a user is allowed access to a page.

Server middleware

We can also add middleware to our server routes. These middleware files are added to a ~/server/middleware directory, and Nuxt will automatically read any files we add. Middleware will run on every request before any other server routes, meaning we can use it for tasks such as logging, checking header data, and adding extra data to the request object. ...