Importante
Visualización de la documentación Beta V1: utilizable pero espere cambios de ruptura. Para la versión estable, vea aquí
¡Ey! ¿Usando React, Vue o Astro? Consulte mi nuevo proyecto Mistcss para escribir un 50% menos de código.
npm install json-server
Crear un archivo db.json
o 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' ,
} ,
}
Puedes leer más sobre el formato JSON5 aquí.
Pásalo a JSON Server CLI
$ npx json-server db.json
Obtenga una API REST
$ curl http://localhost:3000/posts/1
{
" id " : " 1 " ,
" title " : " a title " ,
" views " : 100
}
Ejecutar json-server --help
para una lista de opciones
Patrocinadores |
---|
![]() |
Patrocinadores |
---|
![]() |
Patrocinadores | |
---|---|
![]() | ![]() |
![]() | ![]() |
Conviértase en patrocinador y tenga el logotipo de su empresa aquí
Nota
Este proyecto utiliza la licencia fuente justa. Solo se les pide amablemente a las organizaciones con más de 3 usuarios que contribuyan con una pequeña cantidad a través del patrocinador de patrocinio para el uso. Esta licencia ayuda a mantener el proyecto sostenible y saludable, beneficiando a todos.
Para obtener más información, preguntas frecuentes y la razón detrás de esto, visite https://fair.io/.
Basado en el ejemplo db.json
, obtendrá las siguientes rutas:
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
(predeterminado = 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
Si crea un directorio ./public
, el servidor JSON servirá su contenido además de la API REST.
También puede agregar directorios personalizados usando la opción -s/--static
.
json-server -s ./static
json-server -s ./static -s ./node_modules
id
siempre es una cadena y se generará para usted si falta_per_page
con _page
en lugar de _limit
para paginaciónNetwork tab > throtling
para retrasar las solicitudes en lugar de -opción CLI --delay