mock-json-db-server
v1.0.4
Published
A lightweight local JSON database emulator for mocking RESTful APIs.
Downloads
308
Readme
mock-json-db-server
mock-json-db-server is a simple and lightweight tool to emulate a JSON database. It provides a quick and efficient way to mock API endpoints during development and testing, without setting up a full database backend.
Features
- Serve data from a JSON file as RESTful API endpoints.
- Supports
GET
,POST
,PUT
, andDELETE
operations. - Automatically reloads the database when the JSON file is updated.
- Customizable response delay for simulating network latency.
- Easy to set up and use with minimal configuration.
Installation
To install the package globally:
npm install -g mock-json-db-server
Usage
Run the server with a JSON file:
mock-db-server --file ./path/to/db.json --port 3000 --delay 200
Options
| Option | Alias | Description | Default |
|----------------|-------|--------------------------------------------|--------------|
| --file
| -f
| Path to the JSON database file (required). | None |
| --port
| -p
| Port to run the server. | 3000
|
| --delay
| -d
| API response delay in milliseconds. | 0
|
| --help
| -h
| Display help for the command. | N/A |
Example
Sample JSON File
Create a JSON file named db.json
:
{
"users": [
{ "id": 1, "name": "Alice", "email": "[email protected]" },
{ "id": 2, "name": "Bob", "email": "[email protected]" }
],
"products": [
{ "id": 101, "name": "Laptop", "price": 1200 },
{ "id": 102, "name": "Phone", "price": 800 }
]
}
Start the Server
Run the server:
mock-db-server --file ./db.json --port 4000 --delay 300
The server will start at http://localhost:4000
.
Available Endpoints
Users
Get all users:
GET /users
Get a user by ID:
GET /users/:id
Create a new user:
POST /users Content-Type: application/json Body: { "name": "Charlie", "email": "[email protected]" }
Update a user by ID:
PUT /users/:id Content-Type: application/json Body: { "name": "Charlie Brown" }
Delete a user by ID:
DELETE /users/:id
Products
Get all products:
GET /products
Get a product by ID:
GET /products/:id
Create a new product:
POST /products Content-Type: application/json Body: { "name": "Tablet", "price": 500 }
Update a product by ID:
PUT /products/:id Content-Type: application/json Body: { "price": 600 }
Delete a product by ID:
DELETE /products/:id
Advanced Features
Auto-Reload
- The server automatically reloads the database whenever the JSON file is updated.
- No need to restart the server for changes to take effect.
Simulate Network Latency
- Use the
--delay
option to introduce a delay in responses (e.g.,--delay 300
for 300ms).
Use Cases
Frontend Development: Quickly test your frontend code with a mocked backend.
Integration Testing: Validate API integrations without setting up a real backend.
Prototyping: Test your application idea without building a full backend infrastructure.
Development
If you want to develop or contribute to the package:
Clone the repository:
git clone https://github.com/nandu-99/mock-db-server.git cd mock-db-server
Install dependencies:
npm install
Link the package globally for local testing:
npm link
Test the package:
mock-db-server --file ./example.json --port 3000
Contributing
Contributions are welcome! If you find a bug or have a feature request, feel free to open an issue or submit a pull request.
License
This project is licensed under the MIT License.