Route Handling

Nitro supports file-based routing for your API routes.


Handler files inside routes/ and api/ directory will be automatically mapped to unjs/h3 routes.

Note: api/ is a shortcut for routes/api as a common prefix. However, please note that some deployment providers use app/ directory for their API format. You can simply use the routes/api or srcDir option to move everything under src/ or server/ directory.

Usage

Check out h3 JSDocs for all available utilities.

Examples

Example: Simple API route

// routes/test.tsexport default eventHandler(() => 'Hello World!')

Example: API route with params

// routes/hello/[name].tsexport default eventHandler(event => `Hello ${event.context.params.name}!`)

Example: API route with a specific HTTP request method (get, post, put, delete, options and so on)

// routes/user.get.tsexport default eventHandler(async event => {  return `User profile!`})
// routes/user.post.tsexport default eventHandler(async event => {  const body = await useBody(event)  // TODO: Handle body and update user  return `User updated!`})

Example: Catch all page

// routes/[...].tsexport default eventHandler(event => `Default page`)