Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/2.utils/98.advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ Allowed characters: horizontal tabs, spaces or visible ascii characters: https:/

Prefixes and executes a handler with a base path.

**Example:**

```ts
const app = createApp();
const router = createRouter();
const apiRouter = createRouter().get(
"/hello",
defineEventHandler((event) => {
return "Hello API!";
}),
);
router.use("/api/**", useBase("/api", apiRouter.handler));
app.use(router.handler);
```

<!-- /automd -->

## Cache
Expand Down
16 changes: 16 additions & 0 deletions src/utils/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import { eventHandler } from "../event";

/**
* Prefixes and executes a handler with a base path.
*
* @example
* const app = createApp();
* const router = createRouter();
*
* const apiRouter = createRouter().get(
* "/hello",
* defineEventHandler((event) => {
* return "Hello API!";
* }),
* );
*
* router.use("/api/**", useBase("/api", apiRouter.handler));
*
* app.use(router.handler);
*
* @param base The base path to prefix. When set to an empty string, the handler will be run as is.
* @param handler The event handler to use with the adapted path.
*/
Expand Down