Using Middleware in Nuxt.js Routing

Tutorial 5 of 5

Introduction

The goal of this tutorial is to provide a practical guide on how to use middleware in Nuxt.js for routing control. Middleware in Nuxt.js allows you to define custom functions that can be executed before rendering a page or group of pages.

By the end of this tutorial, you will learn:

  • What middleware is and how it works in Nuxt.js
  • How to create and use middleware in your Nuxt.js applications
  • Best practices when working with middleware

Prerequisites:
- Basic understanding of JavaScript and Vue.js
- Familiarity with Nuxt.js would be beneficial but not mandatory

Step-by-Step Guide

Understanding Middleware

Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle. These functions can execute any code, make changes to the request and the response objects, end the request-response cycle, and call the next middleware function in the stack.

Creating Middleware

To create a middleware, you simply need to add a .js file in the middleware/ directory in your project root. For instance, if you want to create an auth middleware, you would create auth.js in the middleware/ directory.

Example:

// middleware/auth.js
export default function ({ store, redirect }) {
  // If the user is not authenticated
  if (!store.state.authenticated) {
    return redirect('/login')
  }
}

In the above example, the middleware checks if the user is authenticated. If not, it redirects the user to the login page.

Using Middleware

You can use middleware in several ways:

  • Globally - by adding the middleware in the nuxt.config.js
  • For specific routes - by adding the middleware in the pages directory
  • For specific layouts - by adding the middleware in the layouts directory

Code Examples

Global Middleware

To use a middleware globally, you need to add it to the router.middleware in your nuxt.config.js file.

// nuxt.config.js
export default {
  router: {
    middleware: 'auth'
  }
}

In the above example, the auth middleware will run before every route.

Route-Specific Middleware

To use a middleware for specific routes, you need to add a middleware property in your page component.

// pages/index.vue
export default {
  middleware: 'auth'
}

In the above example, the auth middleware will run before the index.vue route is loaded.

Layout-Specific Middleware

To use a middleware for specific layouts, you need to add a middleware property in your layout component.

// layouts/default.vue
export default {
  middleware: 'auth'
}

In the above example, the auth middleware will run before any route using the default layout is loaded.

Summary

In this tutorial, you've learned about middleware in Nuxt.js, how to create middleware, and how to use them globally, for specific routes, and for specific layouts.

To continue learning, you can explore the following resources:
- Nuxt.js Documentation
- Vue.js Documentation

Practice Exercises

  1. Create an admin middleware that redirects to the homepage if the user is not an admin.
  2. Apply the admin middleware to a route of your choice.
  3. Apply the admin middleware to a layout of your choice.

Remember, the key to mastering middleware in Nuxt.js is practice and experimentation. Good luck!