json-server-reset: Middleware for Data Reset and Merge
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.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'db')
cause The `server.db` property, which the `json-server-reset` middleware expects to find, has not been assigned the `json-server` router's database instance.fixAfter creating your `jsonServer.router`, add `server.db = router.db;` before applying the router middleware. -
Error: reset requires a POST request with JSON body
cause The `json-server-reset` middleware received a request to the `/reset` or `/merge` endpoint without a valid JSON body, or with an incorrect HTTP method (e.g., GET).fixEnsure 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":[]}'`.
Warnings
- gotcha The `json-server-reset` middleware must be placed *before* the `jsonServer.router` middleware in your Express application chain. It also requires the `body-parser` middleware to be active to parse incoming JSON payloads for the reset operation.
- gotcha When integrating `json-server-reset` into a custom server setup, you must explicitly assign the `json-server` router's database instance to `server.db` for the reset middleware to function correctly.
- gotcha The package currently uses CommonJS `require` for its main entry points and explicit paths for sub-modules like `merge` and `init-reset`. Attempting to use ESM `import` statements directly might fail depending on your build configuration or Node.js module resolution setup.
Install
-
npm install json-server-reset -
yarn add json-server-reset -
pnpm add json-server-reset
Imports
- reset
import reset from 'json-server-reset';
const reset = require('json-server-reset'); - mergeMiddleware
import { merge } from 'json-server-reset';const mergeMiddleware = require('json-server-reset/src/merge'); - initJsonServerReset
import { initReset } from 'json-server-reset';const initJsonServerReset = require('json-server-reset/src/init-reset');
Quickstart
const jsonServer = require('json-server');
const reset = require('json-server-reset');
const path = require('path');
const fs = require('fs');
const dataFilename = path.join(__dirname, 'data.json');
// Create a simple initial data file if it doesn't exist
if (!fs.existsSync(dataFilename)) {
fs.writeFileSync(dataFilename, JSON.stringify({ todos: [], users: [{ id: 1, name: 'Alice' }] }, null, 2));
}
const server = jsonServer.create();
const router = jsonServer.router(dataFilename);
// json-server-reset requires body-parser, which is included in jsonServer.defaults
server.use(
jsonServer.defaults({
static: '.', // optional static server folder
bodyParser: true,
readOnly: false,
})
);
// Attach the reset middleware before the json-server router
// The middleware expects the router's db to be accessible via server.db
server.use(reset);
server.db = router.db;
server.use(router);
const PORT = 3000;
server.listen(PORT, () => {
console.log(`JSON Server with reset middleware is running on port ${PORT}`);
console.log(`Try:`);
console.log(`- GET http://localhost:${PORT}/todos`);
console.log(`- POST http://localhost:${PORT}/todos text='New Todo'`);
console.log(`- POST http://localhost:${PORT}/reset todos:=[] (using httpie)`);
console.log(`- POST http://localhost:${PORT}/reset users:='[{ "id": 2, "name": "Bob" }]' (using httpie)`);
});