Registry / testing / json-server-reset

json-server-reset

JSON →
library1.6.4jsnpmunverified

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-reset
INSTALL
IMPORT
SIG · JSON-SERVER-RESET
J
json-server-reset
testingjavascriptv1.6.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

reset
const reset = require('json-server-reset');
import reset from 'json-server-reset';
Primarily used in CommonJS style within Node.js servers. The package does not explicitly provide an ESM export for the main middleware, so `require` is the safest option.
mergeMiddleware
const mergeMiddleware = require('json-server-reset/src/merge');
import { merge } from 'json-server-reset';
The 'merge' functionality is exposed as a separate middleware file within the `src` directory, requiring a direct path. It is not a named export from the main package entry point.
initJsonServerReset
const initJsonServerReset = require('json-server-reset/src/init-reset');
import { initReset } from 'json-server-reset';
For initializing resource clearing on server startup, this helper is exposed from a specific file path. Like `merge`, it's not a direct named export.

Demonstrates setting up a `json-server` instance with `json-server-reset` middleware, allowing API data to be reset via a POST request to `/reset`.

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)`); });
Debug
Known issues
gotchaThe `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.
fix
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)`.
affects: >=1.0.0
gotchaWhen 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.
fix
After initializing `const router = jsonServer.router(dataFilename);`, add `server.db = router.db;` before `server.use(router);`.
affects: >=1.0.0
gotchaThe 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.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'db')
The `server.db` property, which the `json-server-reset` middleware expects to find, has not been assigned the `json-server` router's database instance.
fix
After creating your `jsonServer.router`, add `server.db = router.db;` before applying the router middleware.
Error: reset requires a POST request with JSON body
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).
fix
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":[]}'`.
Upgrade
Version history
1.6.4latest on npm
Audit
Dependencies
json-serverrequiredCore dependency for providing the mock API server functionality that this middleware extends.
Agent activity
4 hits · last 30 days
node
4
Resources
json-server-reset — npm install json-server-reset · libregistry