vite-plugin-mock-server is a Vite plugin designed to provide local API mocking capabilities for development servers. It enables developers to define mock APIs using both TypeScript and JavaScript files, automatically hot-reloading them upon modification. The plugin is built with TypeScript, ensuring type safety for mock definitions. A key differentiator is its robust compatibility with `express.js` middlewares, allowing for advanced request processing like body parsing and cookie handling directly within the mock server configuration. It leverages ant-style path matching for flexible URL pattern definition, making it versatile for various API structures. The current stable version is 1.3.1. While no explicit release cadence is stated, it appears to be actively maintained, with updates released as needed to support Vite and address community feedback.
npm install vite-plugin-mock-serverVerified import paths — ran on the pinned version, not inferred.
Demonstrates configuring `vite-plugin-mock-server` in `vite.config.ts` with Vue and common Express-compatible middlewares for body and cookie parsing, alongside an example mock file.
Update `MockFunction` definitions to use `req.params` instead of the `urlVars` argument. For example, `(req, res) => { const userId = req.params?.userId; }`Upgrade your Node.js installation to version 12.0.0 or newer.
Ensure your project's Vite dependency is version 2.0.0 or higher.
Install `body-parser` (or similar) and add `bodyParser.json()` and/or `bodyParser.urlencoded({ extended: true })` to the `middlewares` array in your `vite.config.ts`.Refer to the `@howiefh/ant-path-matcher` documentation for correct pattern syntax (e.g., `*` for single segment wildcard, `**` for multi-segment wildcard, `{var}` for path variables).Verify that the `pattern` in your `MockHandler` definitions correctly matches the incoming request URL, considering Ant-style path matching. Also, check the `urlPrefixes` option in `vite.config.ts` to ensure it covers your API paths.
Install `body-parser` (npm i body-parser -D) and add `bodyParser.json()` and `bodyParser.urlencoded({ extended: true })` to the `middlewares` array in your `vite.config.ts`.Upgrade your Node.js installation to version 12.0.0 or higher. You can use tools like `nvm` to manage Node.js versions.