In this episode, we explore how easy it is to create server-side API endpoints in a Nuxt 3 app. We start off by setting up a /server/api
folder structure, which is where all API endpoint files will live. Then, we create our first endpoint with a super simple hello.js
file that returns a JSON greeting. Accessing the endpoint is just a matter of visiting /api/hello
in the browser – super straightforward!
Next, we move onto a slightly more practical example – a basic stateful counter. We make a new endpoint called increment.js
that returns an incrementing number. Initially, we learn why re-declaring the counter doesn’t work for persisting state, and then we move the counter variable outside of the handler function, so it actually increments with each request (as long as the web server keeps running).
To make things more interactive, we also see how to make a fetch request from a Vue page (using useFetch
) to grab API data and show it in the template. Finally, we touch on how you can access request and response objects if you need them, which opens the door to more advanced backend logic later on.
By the end of this episode, you’ll have a solid grasp of creating simple server-side endpoints in Nuxt, both for quick experiments and for wiring up your app with backend logic—but we’ll save more complex use-cases for future episodes!