Coder Social home page Coder Social logo

simple-json-server's Introduction

simple-json-server

Deploy a local JSON server in a matter of seconds from a JSON file with data and endpoint definitions. No extra dependencies required, just Python.

This project was inspired by tipycode's json-server.

It currently supports only GET and POST methods.

More details can be found here.

Usage

usage: server.py [-h] [-p PORT] [-f FILE] [-u URL]

optional arguments:
  -h, --help            show this help message and exit
  -p PORT, --port PORT  Specify the desired port, defaults to port 80
  -f FILE, --file FILE  File from which to extract routing and data, defaults to db.json
  -u URL, --url URL     Set a fake url for the server, defaults to localhost

As stated above, the default values are 80 for the port, localhost for the hostname and db.json as the configuration file.

Examples

To specify a hostname, port and JSON file from where to extract data and endpoints:

$ python server.py -u climbers-climbers.com -p 5000 -f db.json

Assuming this file is the one used for configuring the server:

Configuration

Routing and data

A single JSON file provides both the server endpoints and the data that will be accessed through that endpoint. The file should contain a single JSON object. For each name/value pair, the name will be an accessible endpoint and the value the data accessed through that endpoint. All endpoints accept, by default, GET requests. However, only endpoints whose value is a list of objects will accept POST requests.

A full example of a configuration file can be found here.

As an example, if the following was defined inside the JSON file:

{
  "/api/metadata": {
    "info": "Provides customer and product info",
    "version": "0.1",
    "endpoints": [
      {
        "route": "/api/customers",
        "params": "id",
        "description": "Get customers"
      },
      {
        "route": "/api/products",
        "params": "normalized_name",
        "description": "Get products"
      }
    ]
  }
}

Then querying [...]/api/metadata will return the value associated with the name. It is also possible to specify a list of objects as the data to be returned instead of a single object. The endpoints whose value is a list of objects are the only ones that will accept and process POST requests. For example:

{
  "api/customers": [
    {
      "id": 1,
      "name": "Alex Honnold",
      "email": "[email protected]",
      "address": "his van"
    },
    {
      "id": 2,
      "name": "Adam Ondra",
      "email": "[email protected]",
      "address": "always traveling"
    },
    {
      "id": 3,
      "name": "Jimmy Webb",
      "email": "[email protected]",
      "address": "Looking for nice boulders"
    }
  ]
}

Then querying [...]/api/customers will return the whole list of customers.

Parameters

For the routes that return a list of objects it is possible to specify a parameter in the configuration file. To do so, the format of the route in the configuration file should be: [...]/path/to/endpoint/:PARAM_NAME. PARAM_NAME should match a property of the stored objects. When the parameter value is set, only the objects whose value matches the specified parameter value will be returned. If no parameter value is set, the complete list of objects will be returned.

As an example, let's say the following has been defined in the configuration file:

{
  "api/customers/:id": [
    {
      "id": 1,
      "name": "Alex Honnold",
      "email": "[email protected]",
      "address": "his van"
    },
    {
      "id": 2,
      "name": "Adam Ondra",
      "email": "[email protected]",
      "address": "always traveling"
    },
    {
      "id": 3,
      "name": "Jimmy Webb",
      "email": "[email protected]",
      "address": "Looking for nice boulders"
    }
  ]
}

Then [...]/api/customers will return the full list of objects while [...]/api/customers/2 will return the customer whose id is 2.

Errors and Exceptions

When an error or exception is raised, the server reply will be in the form:

{
  "result": {
    "code": "ERROR_CODE",
    "message": "ERROR_MESSAGE",
    "description": "ERROR_DESCRIPTION"
  }
}

simple-json-server's People

Contributors

juangallostra avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.