October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Nuxt Server Routes Explained: How Nitro Builds Your API

See how Nuxt 4 turns files in server/api and server/routes into endpoints, how server middleware differs from app route middleware, and how Nitro targets deployment runtimes.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.