Lightweight, simple yet fast and useful tool that help you create a fake rest-api for your frontend by serving a json file.
yarn add @ctuanle/json-serve --dev
or
npm install @ctuanle/json-serve --save-dev
A simple demo on fly.io. Here is the base api of my demo:
https://jss-demo.fly.dev/
yarn jss [json-path] [port] [other-options]
Options | Required | Default | Description |
---|---|---|---|
json-path | no | data.json | Path to your json file |
port | no | 3000 | Port on which server run |
--no-strict | no | false | Turn on no-strict mode |
--readonly | no | false | Turn on readonly mode |
--persist | no | false | Turn on save-change-to-disk |
- GET
- POST
- PUT
- DELETE
- OPTIONS
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).
In this mode, only GET requests are allowed. Default is false.
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.
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
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]
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
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 /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"
}
}
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
}
}
Colorful console logging:
Sample response: