npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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, and DELETE 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

  1. Get all users:

    GET /users
  2. Get a user by ID:

    GET /users/:id
  3. Create a new user:

    POST /users
    Content-Type: application/json
    Body: { "name": "Charlie", "email": "[email protected]" }
  4. Update a user by ID:

    PUT /users/:id
    Content-Type: application/json
    Body: { "name": "Charlie Brown" }
  5. Delete a user by ID:

    DELETE /users/:id

Products

  1. Get all products:

    GET /products
  2. Get a product by ID:

    GET /products/:id
  3. Create a new product:

    POST /products
    Content-Type: application/json
    Body: { "name": "Tablet", "price": 500 }
  4. Update a product by ID:

    PUT /products/:id
    Content-Type: application/json
    Body: { "price": 600 }
  5. 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

  1. Frontend Development: Quickly test your frontend code with a mocked backend.

  2. Integration Testing: Validate API integrations without setting up a real backend.

  3. Prototyping: Test your application idea without building a full backend infrastructure.


Development

If you want to develop or contribute to the package:

  1. Clone the repository:

    git clone https://github.com/nandu-99/mock-db-server.git
    cd mock-db-server
  2. Install dependencies:

    npm install
  3. Link the package globally for local testing:

    npm link
  4. 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.