{"library":"mock-json-server","title":"Mock JSON Server","description":"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.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install mock-json-server"],"cli":{"name":"mock-json-server","version":null}},"imports":["import mockServer from 'mock-json-server'; // For ESM environments","const mockServer = require('mock-json-server');\nmockServer.start(data, port);","npx mock-json-server data.json --port=3000"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const mockServer = require('mock-json-server');\n\nconst mockData = {\n  '/api/users': {\n    'get': {\n      'data': [\n        { 'id': 1, 'name': 'Alice', 'email': 'alice@example.com' },\n        { 'id': 2, 'name': 'Bob', 'email': 'bob@example.com' }\n      ]\n    },\n    'post': {\n      'data': {\n        'message': 'User created successfully', \n        'status': 'success'\n      }\n    }\n  },\n  '/api/products/1': {\n    'get': {\n      'data': { 'id': 1, 'name': 'Laptop', 'price': 1200 }\n    }\n  }\n};\n\nconst PORT = process.env.PORT ?? 8000;\n\nmockServer.start(mockData, parseInt(PORT, 10));\n\nconsole.log(`Mock JSON Server running on http://localhost:${PORT}`);\nconsole.log('GET /api/users - returns a list of users');\nconsole.log('POST /api/users - returns a success message');\nconsole.log('GET /api/products/1 - returns a single product');\n\n// To stop the server programmatically after some time (optional)\n// setTimeout(() => {\n//   mockServer.stop();\n//   console.log('Mock JSON Server stopped.');\n// }, 60000);\n","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}