Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Mock Server

A RESTful mock server built with json-server for rapid API prototyping and testing.

πŸš€ Quick Start

Installation

npm install

Running the Server

Basic Mode

npm start

This starts the server on http://localhost:3000 with live reload when db.json changes.

Custom Mode (with additional features)

npm run start:custom

This runs the custom server with additional middleware and routes.

Development Mode (with delay)

npm run dev

This adds a 1-second delay to all responses to simulate real network conditions.

πŸ“š API Endpoints

The server provides the following REST endpoints:

Users

  • GET /api/users - Get all users
  • GET /api/users/:id - Get user by ID
  • POST /api/users - Create a new user
  • PUT /api/users/:id - Update user
  • DELETE /api/users/:id - Delete user

Posts

  • GET /api/posts - Get all posts
  • GET /api/posts/:id - Get post by ID
  • POST /api/posts - Create a new post
  • PUT /api/posts/:id - Update post
  • DELETE /api/posts/:id - Delete post

Comments

  • GET /api/comments - Get all comments
  • GET /api/comments/:id - Get comment by ID
  • POST /api/comments - Create a new comment
  • PUT /api/comments/:id - Update comment
  • DELETE /api/comments/:id - Delete comment

Products

  • GET /api/products - Get all products
  • GET /api/products/:id - Get product by ID
  • POST /api/products - Create a new product
  • PUT /api/products/:id - Update product
  • DELETE /api/products/:id - Delete product

Custom Endpoints

  • GET /api/health - Health check endpoint
  • GET /api/search?q=query - Search endpoint (placeholder)

πŸ” Query Parameters

json-server supports various query parameters for filtering, sorting, and pagination:

Filtering

GET /api/users?role=developer
GET /api/posts?authorId=1
GET /api/products?inStock=true

Sorting

GET /api/users?_sort=name&_order=asc
GET /api/posts?_sort=createdAt&_order=desc

Pagination

GET /api/users?_page=1&_limit=10

Full-text Search

GET /api/users?q=john

Relationships

GET /api/posts?_expand=user
GET /api/users?_embed=posts

πŸ“ Sample Data

The server comes with sample data including:

  • Users: Developer profiles with roles and contact info
  • Posts: Blog posts with authors and timestamps
  • Comments: Post comments with relationships
  • Products: E-commerce product catalog

πŸ› οΈ Customization

Adding New Resources

  1. Edit db.json to add new data collections
  2. The endpoints will be automatically available

Custom Middleware

Edit server.js to add custom routes and middleware:

// Add custom routes
server.get('/api/custom-endpoint', (req, res) => {
  res.json({ message: 'Custom response' });
});

πŸ”„ Database Reset

To reset the database to its original state:

npm run reset

This will restore db.json from the backup file.

πŸ“‹ Environment Variables

  • PORT - Server port (default: 3000)

πŸ§ͺ Testing the API

You can test the API using curl, Postman, or any HTTP client:

# Get all users
curl http://localhost:3000/api/users

# Get a specific user
curl http://localhost:3000/api/users/1

# Create a new user
curl -X POST http://localhost:3000/api/users \
  -H "Content-Type: application/json" \
  -d '{"name":"New User","email":"new@example.com","role":"tester"}'

# Update a user
curl -X PUT http://localhost:3000/api/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name":"Updated Name","email":"updated@example.com","role":"senior-developer"}'

# Delete a user
curl -X DELETE http://localhost:3000/api/users/1

πŸ“¦ Features

  • βœ… Full REST API with CRUD operations
  • βœ… Automatic route generation from JSON data
  • βœ… Filtering, sorting, and pagination
  • βœ… Full-text search
  • βœ… Relationships and data expansion
  • βœ… CORS support
  • βœ… Custom middleware support
  • βœ… Hot reload during development
  • βœ… Database backup and reset functionality

🀝 Contributing

Feel free to submit issues and enhancement requests!

πŸ“„ License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages