UNPKG

json-api-mocker

Version:

A mock server with visual management interface

185 lines (151 loc) 3.86 kB
# JSON API Mocker A lightweight and flexible mock server with JSON configuration and visual management interface. <p align="center"> <img src="https://img.shields.io/npm/v/json-api-mocker" alt="npm version" /> <img src="https://img.shields.io/npm/l/json-api-mocker" alt="license" /> <img src="https://img.shields.io/npm/dt/json-api-mocker" alt="downloads" /> </p> ## ✨ Features - 🚀 Brand new visual management interface - 🚀 Support both configuration file and UI management - 🔄 Support all common HTTP methods - 📝 Automatic data persistence - 🔍 Built-in pagination support - 🛠 Customizable response structure - 🎭 Powerful data mocking with Mock.js - 📊 Real-time request logs and statistics - 💡 TypeScript support ## 📦 Installation ```bash npm install -g json-api-mocker ``` ## 🚀 Quick Start The server uses fixed ports: - API Server: 35728 - Web UI: 35729 - WebSocket: 35730 ### Method 1: Using Visual Interface (Recommended) 1. Create a `data.json` file: ```json { "server": { "port": 35728, "baseProxy": "/api" }, "routes": [] } ``` 2. Start the server: ```bash # Start server and open browser json-api-mocker -o ``` After starting, the browser will automatically open the management interface, where you can: 1. Visually create and manage APIs 2. View real-time request logs 3. Monitor API call statistics 4. Debug mock data online ### Method 2: Using Configuration File (Compatible with old version) Create a `data.json` file: ```json { "server": { "port": 8080, "baseProxy": "/api" }, "routes": [ { "id": "user-api", "route": { "path": "/users", "methods": { "get": { "status": 200, "response": { "code": 200, "message": "success", "data": { "list|10": [{ "id": "@id", "name": "@name", "email": "@email" }] } } } } } } ] } ``` Then start the server: ```bash json-api-mocker ``` ## 📖 Configuration ### Server Configuration ```json { "server": { "port": 8080, // Server port "baseProxy": "/api" // API base path } } ``` ### API Configuration Each API configuration includes: ```json { "id": "unique-id", // API unique identifier "route": { "path": "/users", // API path (without base path) "methods": { // Supported HTTP methods "get": { "status": 200, // Response status code "headers": { // Custom response headers "Content-Type": "application/json" }, "response": { // Response data (supports Mock.js syntax) "code": 200, "data": { "name": "@name", "age": "@integer(18, 60)" } } } } } } ``` ## 🎮 Visual Interface Features ### 1. API Management - Create, edit, and delete APIs - Support multiple HTTP methods - Visual response data editor - Mock.js syntax hints ### 2. Real-time Logs - Request path and method - Response status and duration - Request parameters recording - Response data viewing ### 3. Statistics Dashboard - Total API count - Request volume monitoring - Average response time - Success rate statistics ## 🔧 Command Line Options ```bash json-api-mocker [options] Options: -p, --port <number> Specify server port (default: 8080) -c, --config <path> Specify config file path (default: data.json) -o, --open Auto open management interface -h, --help Show help information -v, --version Show version number ``` ## 📄 License MIT © [Xiong Haiyin] ## 🙏 Acknowledgments - Thanks to all contributors and users - Special thanks to Mock.js for data mocking support