Api middleware not working on api

I have my api created so im trying to use some middleware. I created a simple file to test, but its not showing when i call endpoint autenticate.js
export default defineEventHandler((event) => {
console.log('New request: ' + event.node.req.url)
})
export default defineEventHandler((event) => {
console.log('New request: ' + event.node.req.url)
})
No description
28 Replies
formigueiro
formigueiroOP2y ago
nitro: {
plugins: ['~/server/index.js']
},
nitro: {
plugins: ['~/server/index.js']
},
nuxt.config.ts
import { Project } from '../../models/Project'

export default defineEventHandler(async (req, res) => {
try {
const projects = await Project.find().limit(6).sort({ 'created_at': -1 })

return projects
} catch (error) {
return { message: 'Failed to process your request, verify syntax is correct' }
}
})
import { Project } from '../../models/Project'

export default defineEventHandler(async (req, res) => {
try {
const projects = await Project.find().limit(6).sort({ 'created_at': -1 })

return projects
} catch (error) {
return { message: 'Failed to process your request, verify syntax is correct' }
}
})
for example when i use this route its not showing nothing from middleware
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
yes im looking inside terminal
formigueiro
formigueiroOP2y ago
No description
formigueiro
formigueiroOP2y ago
any endpoint I call should show up, but it doesn't
formigueiro
formigueiroOP2y ago
my server/index its like that
No description
formigueiro
formigueiroOP2y ago
No description
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
worked? for example, in some page im using const { data: postData, error, pending } = await useFetch('/api/posts/paginate') but its not showing nothing let me try to restart server
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
oh worked
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
i needed to restar server yes hehe on more question
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
I want to apply this middleware only to some routes, is it possible or will it depend on validation? @The_Balm
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
yes, I want to have authenticated routes, I'm using the supabase so I want to validate the token and apply this authenticate to the closed routes. And currently it runs in all
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
but this is not for the server? in this case I want to prohibit access to the endpoint and not the route would there be any way in the endpoint I could add some property to its context that claims to be auth: true for example and in the middleware I validate if it has this?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
for example, here, inside this post file i want to prevent this endpoint with that autenticate middleware
No description
formigueiro
formigueiroOP2y ago
would there be any way to add some auth property to this event? or something like that
formigueiro
formigueiroOP2y ago
No description
formigueiro
formigueiroOP2y ago
i saw this Will I be able to apply something to her context auth: true and validate in the middleware?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
@The_Balm ty my friend
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
i cant use middleware in routes, i could use middlewares inside api endpoints @BrianJM
Want results from more Discord servers?
Add your server