npm install json-server
You can read more about JSON5 format here.
Pass it to JSON Server CLI
$ npx json-server db.json
Get a REST API
$ curl http://localhost:3000/posts/1
{
"id": "1",
"title": "a title",
"views": 100
}
Run json-server --help
for a list of options
Based on the example db.json
, you'll get the following routes:
GET /posts
GET /posts/:id
POST /posts
PUT /posts/:id
PATCH /posts/:id
DELETE /posts/:id
# Same for comments
GET /profile
PUT /profile
PATCH /profile
==
lt
→<
lte
→<=
gt
→>
gte
→>=
ne
→!=
GET /posts?views_gt=9000
start
end
limit
GET /posts?_start=10&_end=20
GET /posts?_start=10&_limit=10
page
per_page
(default = 10)
GET /posts?_page=1&_per_page=25
_sort=f1,f2
GET /posts?_sort=id,-views
x.y.z...
x.y.z[i]...
GET /foo?a.b=bar
GET /foo?x.y_lt=100
GET /foo?arr[0]=bar
GET /posts?_embed=comments
GET /comments?_embed=post
DELETE /posts/1
DELETE /posts/1?_dependent=comments
If you create a ./public
directory, JSON Server will serve its content in addition to the REST API.
You can also add custom directories using -s/--static
option.
json-server -s ./static
json-server -s ./static -s ./node_modules