# Plugin Middlewares

There are 2 types of middlewares in Strapi. Strapi Middlewares (Global) and Route Middlewares

Route Middleware Docs (opens new window)

# Global Plugin Middlewares

To implement a global middleware within a Strapi plugin you first need to make a middleware function

// ./server/middlewares/your-middleware.js

module.exports = async (ctx, next) => {
  console.log("your custom logic")
  await next();

}

You will then export it via your index.js

// ./server/middlewares/index.js
'use strict';
const yourMiddleware = require('./your-middleware');
module.exports = {
  yourMiddleware
};

Then in your Register Lifecycle Hook you will add this code

'use strict';
const middlewares = require('./middlewares');
module.exports = ({ strapi }) => {
    // registeration phase
    strapi.server.use(middlewares.yourMiddleware);
};

View an example here (opens new window)