@chengyixu/mockapi
v1.0.0
Published
Instantly spin up a mock REST API from a JSON file. Zero-config CRUD endpoints, hot reload, request recording, and OpenAPI spec generation.
Maintainers
Readme
@chengyixu/mockapi
Instantly spin up a mock REST API from a JSON file. Zero-config CRUD endpoints with hot reload, request recording, and OpenAPI spec generation.
Install
npm install -g @chengyixu/mockapiQuick Start
Create a db.json file:
{
"users": [
{ "id": 1, "name": "Alice", "email": "[email protected]" },
{ "id": 2, "name": "Bob", "email": "[email protected]" }
],
"posts": [
{ "id": 1, "title": "Hello World", "userId": 1 }
]
}Start the server:
mockapi serve db.jsonYou now have a full REST API:
GET /users # List all users
GET /users/1 # Get user by ID
POST /users # Create a new user
PUT /users/1 # Update a user
DELETE /users/1 # Delete a userFeatures
- Auto CRUD - Endpoints auto-generated from JSON keys
- Hot Reload - Server reloads when the JSON file changes
- Query Filtering -
GET /users?name=Alice - Sorting -
GET /users?_sort=name&_order=desc - Pagination -
GET /users?_page=1&_limit=10 - Custom Routes - Define custom endpoints via config
- Artificial Latency - Simulate slow APIs with
--delay - CORS Enabled - Works with any frontend out of the box
- Colored Logging - Method-colored request logs
- Record Mode - Proxy a real API and save responses
- OpenAPI Spec - Auto-generate OpenAPI 3.0 from your data
- Zero External Deps - Server uses native Node.js
httpmodule
Commands
mockapi serve <file>
Start a mock REST API server.
mockapi serve db.json
mockapi serve db.json --port 8080
mockapi serve db.json --delay 500 # 500ms latency
mockapi serve db.json --routes routes.json
mockapi serve db.json --no-watch # Disable hot reloadOptions:
-p, --port <number>- Port (default: 3000)-H, --host <string>- Host (default: localhost)-d, --delay <ms>- Artificial latency in ms (default: 0)-r, --routes <file>- Custom routes JSON file--no-watch- Disable hot reload
mockapi record <url>
Proxy requests to a real API and record responses.
mockapi record https://jsonplaceholder.typicode.com
mockapi record https://api.example.com -o api-data.json -p 8080Options:
-p, --port <number>- Local proxy port (default: 3000)-H, --host <string>- Host (default: localhost)-o, --output <file>- Output file (default: recorded.json)
mockapi openapi <file>
Generate an OpenAPI 3.0 specification from your data file.
mockapi openapi db.json # Print to stdout
mockapi openapi db.json -o spec.json # Write to fileCustom Routes
Create a routes.json:
[
{
"method": "GET",
"path": "/health",
"status": 200,
"body": { "status": "ok" }
},
{
"method": "POST",
"path": "/login",
"status": 200,
"body": { "token": "mock-jwt-token" }
}
]mockapi serve db.json --routes routes.jsonLicense
MIT
