json server
Crea tu rest api rapidamente:
1.- Posicionate en tu carpeta y ejecuta para instalar json-server (necesitas haber instalado node anteriormente)
npm install -g json-server
2.- Crea el proyecto (package.json) usa -y para omitir las preguntas
npm init -y
2.- Crea el proyecto (package.json) usa -y para omitir las preguntas
npm init -y
3.- Crea el archivo db.json a nivel package .json
{ "posts": [ { "id": 1, "title": "json-server", "author": "typicode" } ], "comments": [ { "id": 1, "body": "some comment", "postId": 1 } ], "profile": { "name": "typicode" } }
4.- Puedes ejecutar el servidor usando
json-server --watch db.json
5.- si deseas agregar el puerto modifica en package.json test por-->
"start": "json-server --watch db.json --port 3001"
6.- Ahora puedes ejecutar
npm start
y veras los siguientes recursos:
http://localhost:3001/posts
http://localhost:3001/comments
http://localhost:3001/profile
http://localhost:3001/comments
http://localhost:3001/profile
-------------
Operaciones get:
http://localhost:3001/posts
o agregas /1 para consultar por 1
[
{
"id": 1,
"title": "json-server",
"author": "typicode"
},
{
"id": 2,
"title": "json-server",
"author": "typicode"
},
{
"title": "json-server",
"author": "typicode",
"id": 3
}
]
-------------
http://localhost:3001/posts
POST (agregas post tomas un objeto) de get y lo añades (puedes eliminar el id)
Request:
{
"title": "json-server",
"author": "typicode"
}
Response:
{
"title": "json-server",
"author": "typicode",
"id": 3
}
-------------
PATH (modificar)
http://localhost:3001/posts/1
Request:
{
"title": "json-serverxxxx",
"author": "typicodexxxx"
}
Response:
{
"id": 1,
"title": "json-serverxxxx",
"author": "typicodexxxx"
}
-------------
DELETE (delete)
http://localhost:3001/posts/3
Response:
{}
busca json server en el internet y crea el archivo
No hay comentarios.