Important
Affichage de la documentation Beta V1 - Utilisable mais attendez-vous à des changements de rupture. Pour une version stable, voir ici
Hé! En utilisant React, Vue ou Astro? Vérifiez mon nouveau projet MISRCSS pour écrire 50% de code en moins.
npm install json-server
Créer un fichier db.json
ou 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' ,
} ,
}
Vous pouvez en savoir plus sur le format JSON5 ici.
Passez-le à JSON Server CLI
$ npx json-server db.json
Obtenez une API REST
$ curl http://localhost:3000/posts/1
{
" id " : " 1 " ,
" title " : " a title " ,
" views " : 100
}
Exécutez json-server --help
pour une liste d'options
Sponsors |
---|
Sponsors |
---|
Sponsors | |
---|---|
Devenez sponsor et demandez au logo de votre entreprise ici
Note
Ce projet utilise la licence Fair Source. Seules les organisations avec plus de 3 utilisateurs sont aimablement invitées à contribuer un petit montant grâce au sponsor de parrainage pour une utilisation. Cette licence aide à garder le projet durable et en bonne santé, ce qui profite à tout le monde.
Pour plus d'informations, la FAQ et la justification derrière cela, visitez https://fair.io/.
Sur la base de l'exemple db.json
, vous obtiendrez les itinéraires suivants:
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
(par défaut = 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 vous créez un répertoire ./public
, JSON Server servira son contenu en plus de l'API REST.
Vous pouvez également ajouter des répertoires personnalisés en utilisant l'option -s/--static
.
json-server -s ./static
json-server -s ./static -s ./node_modules
id
est toujours une chaîne et sera généré pour vous en cas de manque_per_page
avec _page
au lieu de _limit
pour la paginationNetwork tab > throtling
pour retarder les demandes au lieu de --delay
CLI Option