1.0.3-beta • Published 2 years ago

@ctuanle/json-serve v1.0.3-beta

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

JSON-Serve

Lightweight, simple yet fast and useful tool that help you create a fake rest-api for your frontend by serving a json file.

Installation

yarn add @ctuanle/json-serve --dev

or

npm install @ctuanle/json-serve --save-dev

Demo

A simple demo on fly.io. Here is the base api of my demo:

https://jss-demo.fly.dev/

Usage

yarn jss [json-path] [port] [other-options]
OptionsRequiredDefaultDescription
json-pathnodata.jsonPath to your json file
portno3000Port on which server run
--no-strictnofalseTurn on no-strict mode
--readonlynofalseTurn on readonly mode
--persistnofalseTurn on save-change-to-disk

Available methods

  • GET
  • POST
  • PUT
  • DELETE
  • OPTIONS

No-strict mode

By default, you can only post/put/delete to array data. But in no-strict mode, these action are allowed with object type (key-value).

Read-only mode

In this mode, only GET requests are allowed. Default is false.

Persist

Save changes created by POST/PUT/DELETE to your json file. Default is false, so changes are kept only on memory and will be deleted when you turn server off.

Example

You've create a data.json file:

yarn jss data.json 3000

Or if you're too lazy and you don't specify a path, a prompt will appear and ask you if you want to create one with pre-defined data for you:

yarn jss

You want to serve your json file and persist changes:

yarn jss data.json 3000 --persist

Details

If your json file contains this content:

{
  "method": [
    {
      "name": "GET"
    },
    {
      "name": "POST"
    }
  ],
  "protocol": {
    "1": "HTTP",
    "2": "UDP"
  }
}

All available routes are:

/method
/method/[index]
/protocol
/protocol/[index]

GET

To get "protocol", you can go with GET /protocol.

Or to get all methods, go with GET /method.

Plus, with array data, you can filter it with query, for example, to get all method that name is "GET", GET /method?name=GET

POST

With post request, you can update your json file.

If the target resources is an array, received data will be pushed into the array.

Ex: POST /method with body {"name": "PUT"} will turn above data into

{
  "method": [
    {
      "name": "GET"
    },
    {
      "name": "POST"
    },
    {
      "name": "PUT"
    }
  ],
  "protocol": {
    "1": "HTTP",
    "2": "UDP"
  }
}

Please note that if you edit json file manually while the server is running, edited data won't be seen by the server. In that case, restart the server.

PUT

PUT /protocol/0 with body {"name": "PATCH"} and PUT /protocol/2 with body "TCP" will turn above data into:

{
  "method": [
    {
      "name": "PATCH"
    },
    {
      "name": "POST"
    },
    {
      "name": "PUT"
    }
  ],
  "protocol": {
    "1": "HTTP",
    "2": "TCP"
  }
}

DELETE

With DELETE requests, you can delete a specific data.

DELETE /method/2 and DELETE /protocol/2 will turn above data into:

{
  "method": [
    {
      "name": "PATCH"
    },
    {
      "name": "POST"
    },
    null
  ],
  "protocol": {
    "1": "HTTP",
    "2": null
  }
}

Screenshots

Colorful console logging:

Logging Console

Sample response:

Server response

1.0.3-beta

2 years ago

1.0.2-beta

2 years ago

1.0.1-beta

2 years ago

1.0.0

2 years ago