Skip to content
tezvyn:

Nuxt Server Routes: Your Backend in a Folder

Source: nuxt.comMediumHow cards are made

Nuxt Server Routes: Your Backend in a Folder

Nuxt Server Routes let you build a backend inside your frontend project. Create API endpoints by adding files to the server/api directory, which automatically get an /api prefix. The common footgun is forgetting this prefix difference with server/routes.

Why it exists

To simplify full-stack development by co-locating backend API logic within the same Nuxt project, eliminating the need for a separate server application for many common use cases. This file-based routing system provides a "zero-config" way to create server endpoints.

The mental model

Think of the server/ directory as a mini-backend living inside your Nuxt application. Instead of configuring a router, you just create files. A file named server/api/users.ts automatically becomes an API endpoint available at /api/users. It's convention over configuration for your server logic.

How it works

Nuxt scans the server/ directory for files and maps them to routes. Each file must export a default handler function, typically wrapped in defineEventHandler(). This handler receives an event object and can return JSON data or a Promise. The server/api folder automatically prefixes routes with /api, while the server/routes folder creates routes at the root level (e.g., server/routes/ping.ts becomes /ping). Nuxt also supports dynamic routes using bracket notation (e.g., [id].ts) and server middleware that runs on every request.

When to use it

Use server routes for creating REST APIs for your frontend to consume via useFetch. It's ideal for handling form submissions, fetching data from a database, or integrating with third-party services where you need to hide secret keys. The server/middleware directory is perfect for cross-cutting concerns like request logging or adding authentication context to a request.

When not to use it

For extremely complex or microservice-based architectures, a dedicated standalone backend framework might be more appropriate. While Nuxt's server engine (Nitro) is powerful, server routes are not designed to replace a full-featured backend for large, enterprise-scale applications with specialized needs. Also, avoid putting logic in middleware that should return a response; middleware should only inspect or modify the request, not terminate it.

One canonical example

To create a dynamic user profile endpoint, you would create a file at server/api/users/[id].ts. Inside, you'd define a handler to access the dynamic parameter from the event context: export default defineEventHandler((event) => { const userId = event.context.params.id; return { userId: userId, name: 'Jane Doe' }; }); A useFetch('/api/users/123') call from a component would then return { "userId": "123", "name": "Jane Doe" }.

Interview question

What is the key difference in how routes are exposed when placing a file in server/api versus server/routes?

  • a.Files in server/api require explicit handler registration, while server/routes use file-based routing automatically.
  • b.Files in server/api are only accessible from the client, while server/routes are for internal server-to-server communication.
  • c.Files in server/api are used for RESTful APIs, while server/routes are for GraphQL endpoints.
  • d.Files in server/api are automatically prefixed with /api, whereas server/routes are exposed at the root level.Correct
Why?

The card explicitly states that "The server/api folder automatically prefixes routes with /api, while the server/routes folder creates routes at the root level." This is the fundamental difference in how their paths are exposed. Option B is incorrect because both server/api and server/routes define server endpoints accessible from the client or other services.

Just read this? Test yourself on what you have been reading.

Read the original → nuxt.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles