useRequestEvent

Source
Access the incoming request event with the useRequestEvent composable.

Within the Nuxt context you can use useRequestEvent to access the incoming request.

// Get underlying request event
const event = useRequestEvent()

// Get the path of the incoming request
const path = event?.url.pathname

// Read a request header
const userAgent = event?.req.headers.get('user-agent')
In the browser, useRequestEvent will return undefined.

Type

function useRequestEvent (nuxtApp?: NuxtApp): NuxtRequestEvent | undefined

NuxtRequestEvent is the event in the shape your configured server builder provides. With the default @nuxt/nitro-server, that is h3's H3Event.

When no server builder contributes an event type, the event resolves to RequestEvent, the web-standard part every server runtime provides:

PropertyTypeDescription
reqRequestThe incoming request, including its headers, method and body.
urlURLThe parsed request URL.
res{ status?, statusText?, headers }The response status and headers to be sent.
contextRequestEventContextPer-request state, including Nuxt's own state under context.nuxt.

Code that should work whichever server builder is configured (a module's server handler, for example) should only read these properties.

Read more about writing portable server code with nuxt/server.