vite-plugin-api-routes is a Vite.js plugin designed to streamline backend API development by implementing a file-system based routing approach, reminiscent of Next.js API Routes. It automatically converts a designated directory structure into API endpoints, enhancing project organization and visibility for Node.js and Express applications integrated with Vite. The package is currently in a beta phase, with version `1.3.0-beta1`, indicating active development. Key differentiators include two distinct routing modes: "ISOLATED" where each HTTP method resides in its own file for explicit endpoint declaration, and "LEGACY" allowing multiple methods within a single file for simpler APIs. It also provides a priority mapping system to precisely control middleware execution order, supporting advanced API configurations. While in beta, its robust features aim to simplify full-stack Vite projects.
npm install vite-plugin-api-routesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure `vite-plugin-api-routes` in `vite.config.ts` to enable file-system based API routing. It sets up the plugin to serve routes from `src/api` under the `/api` URL prefix using `ISOLATED` mode, and includes a simple `GET` route handler for `/api/hello`.
Review release notes diligently for each update and consider locking dependency versions to specific beta releases for stability in production environments, though this is generally not recommended for beta software.
Clearly define and consistently apply one routing mode (via the `mode` option in the plugin configuration) across your entire API directory structure. The default mode is `ISOLATED`.
Carefully define priorities, especially for `USE` methods, and test middleware chains thoroughly. Ensure unique and logical priority values to prevent conflicts and ensure middleware executes as intended.
Verify the `dir` path in `vite.config.ts` is correct and contains valid API route files (e.g., `GET.ts`, `index.ts`). It's recommended to use `path.resolve(process.cwd(), 'your/path')` for robust absolute path resolution.
Check that the `base` option (e.g., `/api`) matches the URL prefix used in your client-side requests (e.g., `fetch('/api/my-route')`). Also, ensure your route files (e.g., `src/api/my-route/GET.ts`) correctly map to the desired endpoint according to the `ISOLATED` or `LEGACY` routing mode.Ensure your API route files `export default` an Express-compatible handler function, e.g., `export default (req, res) => { res.status(200).json(...) }`. Also, ensure `express` is installed if you are using its types or features directly.