json-server-reset is a middleware for the popular json-server library, enabling developers to programmatically reset or merge the in-memory database of a running mock API. The current stable version is 1.6.4. Releases are infrequent but consistent, primarily driven by dependency updates and occasional feature additions, as seen with features like 'merge mode' and 'reset module' in v1.5.0 and v1.6.0. Its key differentiator is providing a simple HTTP endpoint (`/reset` or `/merge`) to modify the mock database state during testing or development, which is crucial for maintaining consistent test environments or quickly iterating on frontend changes without restarting the mock server. It integrates directly into the json-server middleware chain, requiring no complex setup beyond standard Express middleware practices. This package is vital for scenarios where a dynamic, resettable mock backend is needed, especially in automated testing suites or rapid prototyping.
npm install json-server-resetVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up a `json-server` instance with `json-server-reset` middleware, allowing API data to be reset via a POST request to `/reset`.
Ensure `server.use(reset)` comes before `server.use(router)`. If not using `jsonServer.defaults({ bodyParser: true })`, manually add `server.use(express.json())` and `server.use(express.urlencoded({ extended: true }))` before `server.use(reset)`.After initializing `const router = jsonServer.router(dataFilename);`, add `server.db = router.db;` before `server.use(router);`.
Use `const reset = require('json-server-reset');` for the main middleware. For `merge` and `init-reset`, use `require('json-server-reset/src/merge')` and `require('json-server-reset/src/init-reset')` respectively.After creating your `jsonServer.router`, add `server.db = router.db;` before applying the router middleware.
Ensure you are sending a `POST` request to `/reset` or `/merge` and that the request body is valid JSON. Use tools like `httpie` with `key:=value` syntax or `curl -X POST -H "Content-Type: application/json" -d '{"todos":[]}'`.