simple-node-mockserver
v1.0.0
Published
Simple mock server
Downloads
5
Maintainers
Readme
MockServer
MockServer is an npm package that helps you quickly create a mock server, acting as a mock database without any complex DB connections.
This library auto-generates REST APIs for CRUD operations, making mocking fast, easy, and SIMPLE.
Features
- Multiple Storage Backends:
- In-memory (RAM)
- File-based (JSON/text)
- Excel file (each table as a sheet in a temp
.xlsxfile)
- Auto-generated REST API:
- CRUD endpoints for each table
- Seed and purge endpoints for quick data resets
- Easy Table Definition:
- Define tables and seed data in a single config array
Usage
1. Install dependencies
npm install2. Define your tables and seed data
In your app.js:
const myTables = [
{
name: "Users",
seed: [
{ id: 1, first_name: "Jhon", last_name: "Dantas", company_name: null },
// ... more seed data ...
]
}
];3. Choose your storage backend
In your app.js, select one of:
const { SimpleMockServer, DBFile, DBRam, DBFunction } = require("mockserver");
const DBExcel = require("mockserver/database/db_excel");
const dbInstance = new DBExcel(); // or new DBFile(), new DBRam()4. Start the server
node app.jsServer runs on port 2000 by default.
5. API Endpoints
For each table (e.g., Users):
GET /Users— Get all dataGET /Users/:id— Get data by idPOST /Users— Add new data (expects JSON body)PUT /Users/:id— Edit data by id (expects JSON body)DELETE /Users/:id— Delete data by idGET /Users/seed— Reset table to seed dataGET /Users/purge— Clear all data in table
Excel Backend Details
- Uses a temporary
.xlsxfile in your system temp directory. - Each table is a separate sheet.
- Data is stored as rows in the sheet.
- Requires the
xlsxnpm package.
Example Request
curl -X POST http://localhost:2000/Users \
-H "Content-Type: application/json" \
-d '{"id":7,"first_name":"Jane","last_name":"Doe","company_name":"Acme Inc."}'License
MIT
