json server
v1.0.0-beta.3
重要的
查看Beta V1文档 - 可用但期望破裂的变化。对于稳定版本,请参阅此处
嘿!使用React,Vue还是Astro?检查我的新项目mistcss,以少50%的代码编写。
npm install json-server
创建db.json
或db.json5
文件
{
"posts" : [
{ "id" : " 1 " , "title" : " a title " , "views" : 100 },
{ "id" : " 2 " , "title" : " another title " , "views" : 200 }
],
"comments" : [
{ "id" : " 1 " , "text" : " a comment about post 1 " , "postId" : " 1 " },
{ "id" : " 2 " , "text" : " another comment about post 1 " , "postId" : " 1 " }
],
"profile" : {
"name" : " typicode "
}
}
{
posts : [
{ id : '1' , title : 'a title' , views : 100 } ,
{ id : '2' , title : 'another title' , views : 200 } ,
] ,
comments : [
{ id : '1' , text : 'a comment about post 1' , postId : '1' } ,
{ id : '2' , text : 'another comment about post 1' , postId : '1' } ,
] ,
profile : {
name : 'typicode' ,
} ,
}
您可以在此处阅读有关JSON5格式的更多信息。
将其传递给JSON服务器CLI
$ npx json-server db.json
获取休息API
$ curl http://localhost:3000/posts/1
{
" id " : " 1 " ,
" title " : " a title " ,
" views " : 100
}
运行json-server --help
以获取选项列表
赞助商 |
---|
![]() |
赞助商 |
---|
![]() |
赞助商 | |
---|---|
![]() | ![]() |
![]() | ![]() |
成为赞助商,并在此处拥有您的公司徽标
笔记
该项目使用公平源许可证。只有拥有3个以上用户的组织才能通过赞助商的使用来贡献少量。该许可有助于使项目保持可持续性和健康,从而使所有人受益。
有关更多信息,FAQS和此背后的理由,请访问https://fair.io/。
根据示例db.json
,您将获得以下路线:
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
(默认= 10) GET /posts?_page=1&_per_page=25
_sort=f1,f2
GET /posts?_sort=id,-views
xyz..
xyz[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
如果您创建./public
目录,则JSON服务器除REST API外还将提供其内容。
您还可以使用-s/--static
选项添加自定义目录。
json-server -s ./static
json-server -s ./static -s ./node_modules
id
始终是一个字符串,如果丢失,将为您生成_per_page
与_page
代替_limit
进行分页Network tab > throtling
来延迟请求,而不是--delay
CLI选项