nuxt.jsnuxt3.js

What is the difference between the routes and api functionality in a Nuxt server?


When building a Nuxt application, the server can provide routes and among them, the api one.

Is there a technical difference between a generic route (~/server/routes/hellohttp://localhost:3000/hello) and its api specific case (~/server/api/bonjourhttp://localhost:3000/api/bonjour)?

Specifically - is it semantics, or are the requests handled differently?


Solution

  • They're pretty much handled the same under the hood by Nitro. The main differences are mostly about semantics and organization. If you put files in server/api, it's just a clear way to tell other devs (and yourself) that these endpoints are meant for API stuff, like handling JSON or similar responses. Plus, you might get some automatic goodies like JSON body parsing. But if you drop your endpoint in server/routes, it's just as flexible but doesn't come with those extra conventions, it's more generic. So, it's less about how the request is processed and more about keeping your project organized.