Mock JSON Server is a Node.js package designed to create simple, configurable mock REST APIs using a single JSON file. Released as version 1.3.6, with its last update approximately six years ago, it offers a straightforward solution for frontend developers needing temporary API backends during development or for prototyping. Unlike more feature-rich alternatives that dynamically generate endpoints from top-level JSON keys, `mock-json-server` requires explicit path and HTTP method definitions within its configuration JSON. It provides basic CRUD-like capabilities by returning predefined data for specific routes and methods, and supports programmatic control (start, reload, stop) in addition to a command-line interface. Its primary differentiators are its minimal setup and explicit configuration, making it suitable for simple mocking scenarios where fine-grained control over static responses per route and method is desired.
npm install mock-json-serverVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically start a mock server using `mock-json-server` with a predefined JSON data structure, serving multiple routes and HTTP methods. It initializes the server on a specified port and logs available endpoints.
For new projects or if advanced features like dynamic routing, database persistence, or middleware are required, consider actively maintained alternatives like `json-server` (typicode) or `Mock Service Worker`.
Ensure your `data.json` or programmatic data object strictly follows the `{ '/path': { 'method': { 'data': ... } } }` format for each desired endpoint and method. Incorrect structures will result in 404s or unexpected responses.Never deploy `mock-json-server` to a production environment. For development, ensure it's only accessible from trusted networks or behind appropriate firewall rules.
When using the CLI (`npx mock-json-server data.json`), changes to `data.json` require a manual restart of the server to take effect. If programmatic reloading is needed, use the `mockServer.reload(newData)` function directly in your script.
Use the `--port` flag (e.g., `mock-json-server data.json --port=3001`) or set the `PORT` environment variable (`mockServer.start(data, parseInt(process.env.PORT || '3000', 10));`) to use a different, available port.
Verify that the requested path and HTTP method (e.g., GET, POST) are explicitly defined in your mock data, following the expected `{ '/path': { 'method': { 'data': ... } } }` structure.Correct the JSON syntax in your `data.json` file. Use a JSON linter or validator to identify and fix errors. Ensure it's pure JSON, not JSON5 or JavaScript objects.
No dependency data recorded yet.