NuxtN
Nuxt2y ago
Phillip

appMiddleware in routeRules not working?

I want to run a middleware when my API in /server/api/admin/text-snippet.post.ts is being called but it doesn't run the middleware at all.

Nuxt 3.12.2

nuxt.config.ts

routeRules: {
    "/portal": { redirect: START_PAGE_AFTER_LOGIN },
    '/portal/**': { ssr: false },
    '/server/api/admin/**': { appMiddleware: 'mytest' }
},


middleware/mytest.ts
export default defineNuxtRouteMiddleware(async (to, _from) => {
  console.log('getting in middleware') // <-- the issue: this is never being called
  return false
})


server/api/admin/text-snippet.post.ts
export default defineEventHandler(async (event) => {
    console.log('getting in API') // <-- getting called
    return { success: true }
})


Then, I access the API via Postman. Method: Post. URL: http://localhost:3000/api/admin/text-snippet
The response is { success: true }

What am I missing or is this a bug?
Was this page helpful?