Nuxt has built-in support to create server endpoints. You can use it to fetch your data from your database, request other API, return static content, think like express.js with super powers, thanks to Nitro and UnJS ecosystem.
This is a basic example of directory structure for your <app>/server/
directory:
•Hello world route
We will create a /api/hello
route that will return a greeting message.
You can test your API route by visiting http://localhost:3000/api/hello?from=world
in your browser or by using curl
:
•Dynamic routes
You can also define dynamic routes by using the [paramName]
syntax in either files or directories. For example, if you want to create a route that will have a dynamic id
parameter, you can create a file named <app>/server/api/product/[id].ts
or a directory named [id]
like <app>/server/api/product/[id]/index.ts
.
In addition to parameters, you can add suffixes to your dynamic routes. For example, you can create a route that will only accept POST
requests by creating a file ending with .post.ts
.
Useful resources:
Now that you have a page and an API route, you can connect them together. We will request the hello API route from the page created in the previous section and display the result.
We've seen that using the <BaseInput> , <BaseCard> and <BaseParagraph> components was very easy. You may also want to take a look at how we do form validation and explore form pages from the demo to learn more about how to build forms with Tairo.
Useful resources: