Your first API route

Similar to pages, you can define event handlers in your .app/server/api directory. These event handlers are automatically registered as API routes by Nitro.

Hello world route

We will create a /api/hello route that will return a greeting message.

.app/server/api/hello.ts

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.

.app/server/api/add-to-cart/[id

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.

.app/pages/index.vue

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 Apollux.


Useful resources: