N
Nuxt5mo ago
funk2351

Server middleware for specific route

Hi, i have this structure: - /pages/page/[...slug].vue - /pages/component/[...slug].vue And i want to make server middleware only for these routes, how can i do this? I was trying with this solution: - /server/routes/page/[...slug].get.ts - /server/routes/page/[...slug].post.ts but it's nor working. Can you help me?
2 Replies
Archerist
Archerist5mo ago
well, server middlewares should be in server/middleware folder, aside from that you can easily do something like this:
export default defineEventHandler(async (event) => {
let URL = getRequestURL(event)
if (URL.pathname.startsWith("/foo/bar")) {
///Do stuff
}
}
export default defineEventHandler(async (event) => {
let URL = getRequestURL(event)
if (URL.pathname.startsWith("/foo/bar")) {
///Do stuff
}
}
https://nuxt.com/docs/guide/directory-structure/server#server-middleware also if i understand correctly you have vue components inside your pages folder. you should move them out of the pages folder and into components folder
funk2351
funk2351OP5mo ago
No, no, 'component' is just a page 🙂 Thanks for your help!
Want results from more Discord servers?
Add your server