Free tools Windows power users keep installed
One-click scans. No signup required.
In Nuxt 4, a file’s location under server/ determines whether Nitro exposes it as an API endpoint, a route without the /api prefix, or request middleware. Nuxt scans these server directories, registers the handlers, and uses Nitro—with h3 for its API and middleware layer—to serve them.
How do I add an API route in Nuxt?
Create a file under server/api and export a default event handler. Nuxt automatically scans the server directory and registers the handler; the api directory adds the /api prefix to its public path.
// server/api/hello.ts
export default defineEventHandler(() => {
return { message: 'Hello' }
})
This file serves /api/hello. The Nuxt 4 server directory reference documents defineEventHandler() and its alias, eventHandler(), as ways to define a handler.
For ordinary JSON endpoints, return an object or array. Nitro serializes the returned data as JSON and waits for returned promises. Returning a value also lets Nuxt generate route typings that $fetch and useFetch can use; writing directly to Node response APIs is possible, but skips that straightforward return-value pattern. The Nuxt server-engine concept guide describes Nitro and h3’s role in this request handling.
#1 Best Overall
How do I add a server route without the /api prefix?
Put the handler under server/routes instead. The directory choice determines the public path:
| File | Public path | Use it for |
|---|---|---|
server/api/hello.ts |
/api/hello |
An API endpoint with the conventional /api prefix |
server/routes/hello.ts |
/hello |
A server route without that prefix |
Both files export a default handler. Nuxt’s server reference cautions that dynamic server routes do not currently support every dynamic routing feature available to pages, so do not assume page-routing behavior carries over unchanged.
Rank #2
Does route middleware run for API routes?
No. Nuxt has two distinct middleware layers. App route middleware runs in the Vue application as a navigation guard; it does not run for server routes such as /api/*. Server middleware, by contrast, runs on every incoming server request before the matching route handler. Nuxt explains the distinction in its routing guide and server directory reference.
Use server middleware for request-wide work
Place a middleware file in server/middleware when logic should inspect or annotate requests across routes—for example, logging, adding headers, or attaching values to the event context. Middleware should not return a response, close the request, or otherwise take ownership of the response. If it must reject a request, throw an error.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesRank #3
Keep endpoint-specific behavior in the handler
Logic that belongs to one endpoint can live in that endpoint’s handler. This keeps the cross-cutting work in middleware and the route’s own behavior with the route, rather than relying on Vue navigation guards to protect or process an API request.
What belongs in the Nuxt server context?
Nuxt also scans server/plugins for Nitro plugins, which can extend runtime behavior and hook into lifecycle events. Reusable server-only helpers belong in server utilities. Keep server-only modules out of Vue components and app-side code, and avoid importing Vue components or composables into server routes; Nuxt documents these as separate contexts in its directory structure reference.
The #server alias for server code is available in Nuxt 4.3 and later, according to that directory reference. The Nuxt Kit Nitro reference also lists server/api, server/routes, server/middleware, and server/utils among the built-in scanned server areas. Module authors can extend registration with Kit’s addServerHandler or addServerScanDir; these are extension APIs, not steps needed to create a routine application route.
How does Nitro build and deploy a Nuxt server?
Nitro is Nuxt’s server engine. It packages server routes and runtime behavior for a target such as a Node.js server, static pre-rendering, a serverless platform, or an edge/CDN environment. The right build target depends on the APIs and dependencies your handlers need as well as the host’s supported runtime; consult Nuxt’s deployment guide for the appropriate preset and constraints.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Best Value
Run the Node.js server output
For the Node server preset, nuxt build creates .output/server/index.mjs. Run that built server with:
NODE_ENV=production node .output/server/index.mjs
Select a preset for the actual host
Nitro accepts a deployment preset in configuration or through NITRO_PRESET at build time. Preset availability and provider requirements can change, so choose and verify the target against the current deployment documentation rather than assuming a build for one runtime will suit another.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




