N
Nuxt8mo ago
Ziko

Getting Nuxt page (URL/Name) in API server request?

Hi All I'm having trouble getting the nuxt component/page full path dynamically in my API eventhandler, without having to pass it as some sort of data in the request. I've tried checking all the composables, but the one that works for me in middleware doesn't work in the API server: useRequestURL(); Any ideas?
9 Replies
Ziko
ZikoOP8mo ago
More info: The error i get while trying to useRequestUrl, is: 'Vue app aliases are not allowed in server runtime.'
numbani
numbani8mo ago
do you mean the params?
export default defineEventHandler(async (event) => {
const user =event.context.sessions?event.context.sessions.user.id: event.context.params?.user
const service = event.context.params?.service
if (!user) {
throw new Error("User not found")
}
if (!service) {
throw new Error("Service not found")
}
console.log("user:",user)
console.log("service:",service)
export default defineEventHandler(async (event) => {
const user =event.context.sessions?event.context.sessions.user.id: event.context.params?.user
const service = event.context.params?.service
if (!user) {
throw new Error("User not found")
}
if (!service) {
throw new Error("Service not found")
}
console.log("user:",user)
console.log("service:",service)
Ziko
ZikoOP8mo ago
Let say i have page: pages/abc.vue, that requests nuxt API server: /api/def In def eventHandler i want to know what page requested the API, but i can at most only get origin: localhost:4200 The href for the event, is also only the api endpoint I want the page origin URL path
netwrx
netwrx8mo ago
You can't get the origin path through raw http I would suggest using cookies if you really need the origin path (set current path before sending request)
Ziko
ZikoOP8mo ago
Is it not possible to set some context on the event.context in the eventHandler ? I see this as the least worse idea ATM
netwrx
netwrx8mo ago
event.context is server side only, don't think that would work
Ziko
ZikoOP8mo ago
Just seems like something that should be easily done
netwrx
netwrx8mo ago
I myself actually use query parameters So you send the current path as a query to the server Just make sure you deal with security stuff like CSRF
Ziko
ZikoOP8mo ago
I'm trying to do a pseudo CMS, so i need to validate the page url. Which is why i dont want to do it in query/body
Want results from more Discord servers?
Add your server