diff --git a/docs/3.config/0.index.md b/docs/3.config/0.index.md index 25851f4e7f..d9e3fa967b 100644 --- a/docs/3.config/0.index.md +++ b/docs/3.config/0.index.md @@ -245,6 +245,12 @@ Default: `/`{lang=ts} (or `NITRO_APP_BASE_URL` environment variable if provided) Server's main base URL. +### `apiBaseURL` + +- Default : `/api` + +Changes the default api base URL prefix. + ### `handlers` Server handlers and routes. @@ -408,6 +414,18 @@ Project source directory. Same as `rootDir` unless specified. Helpful to move co List of directories to scan and auto-register files, such as API routes. +### `apiDir` + +- Default : `api` + +Defines a different directory to scan for api route handlers. + +### `routesDir` + +- Default : `routes` + +Defines a different directory to scan for route handlers. + ### `buildDir` - Default: `.nitro` diff --git a/src/scan.ts b/src/scan.ts index b69414c490..2b1c7acf23 100644 --- a/src/scan.ts +++ b/src/scan.ts @@ -18,8 +18,12 @@ export async function scanHandlers(nitro: Nitro) { const middleware = await scanMiddleware(nitro); const handlers = await Promise.all([ - scanServerRoutes(nitro, "api", "/api"), - scanServerRoutes(nitro, "routes", "/"), + scanServerRoutes( + nitro, + nitro.options.apiDir || "api", + nitro.options.apiBaseURL || "/api" + ), + scanServerRoutes(nitro, nitro.options.routesDir || "routes"), ]).then((r) => r.flat()); nitro.scannedHandlers = [ @@ -49,7 +53,7 @@ export async function scanMiddleware(nitro: Nitro) { export async function scanServerRoutes( nitro: Nitro, - dir: "routes" | "api", + dir: string, prefix = "/" ) { const files = await scanFiles(nitro, dir); diff --git a/src/types/nitro.ts b/src/types/nitro.ts index aa9b836959..ae731a53cb 100644 --- a/src/types/nitro.ts +++ b/src/types/nitro.ts @@ -255,6 +255,8 @@ export interface NitroOptions extends PresetOptions { rootDir: string; srcDir: string; scanDirs: string[]; + apiDir: string; + routesDir: string; buildDir: string; output: { dir: string; @@ -372,6 +374,7 @@ export interface NitroOptions extends PresetOptions { // Routing baseURL: string; + apiBaseURL: string; handlers: NitroEventHandler[]; routeRules: { [path: string]: NitroRouteRules }; devHandlers: NitroDevEventHandler[];