czwartek, 17 marca 2016

Really simple JSON server

As some of you may already know there's this json-server project which aims to provide an easy way to create fake servers returning JSON responses over HTTP. This is very great project, but if you need something simpler that does not necessarily follow all of the REST rules, then you're out of luck.

For instance, if you use json-server with PUT/POST requests then underlying database will change.

really-simple-json-server, on the other hand, is an effort to create really simple JSON server. To actually show you how simple it is, let's have a look at example routes (example.json).

{
    "/config": {"avatar_upstream_server": "172.17.42.42"},
    "/u/1/friends": ["slavko", "asia", "agata", "zbyszek", "lucyna"],
    "/u/2/friends": ["adam", "grzegorz"],
    "/u/3/friends": [],
    "/u/4/friends": ["slavko"]
}

Then, assuming all dependencies (see below) are installed it's all about starting a server:

$ ./server.py --port 1234 example.json

And we can start querying the server!

$ curl http://localhost:1234/config
{"avatar_upstream_server": "172.17.42.42"}

The project uses Python 3.5.1 along with aiohttp package. It is shipped with Docker image, so it's pretty easy to start hacking.

$ docker build -t szborows/python351_aiohttp .
$ docker run -it -v $PWD:/app:ro szborows/python351_aiohttp /bin/bash -c "/app/server.py --port 1234 /app/example.json"

1 komentarz:

  1. json-server
    REST API with zero cod­ing in less than 30 seconds.
    This mod­ule by typi­code helps front-end de­velopers who need a quick back-end for pro­to­typ­ing and mock­ing!

    Get it:

    Either as a CLI app: npm install -g json-server

    or as a dep for your script: npm install --save json-server

    Sample usage:

    As­sume you have a json.db like:

    {
    "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
    ],
    "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
    ]
    }
    If you are in­stalled mod­ule glob­ally, then do a :

    $ json-server json.db

    # or

    $ json-server http://example.com/file.json
    as mentioned here - http://www.credosystemz.com/training-in-chennai/best-angularjs-training-in-chennai/

    OdpowiedzUsuń