JSON Server
CLI
/codes/package/json-server/db.json
{
"hosts": [
{
"id": 1,
"name": "Google Search",
"address": "www.google.com"
},
{
"id": 2,
"name": "Google DNS",
"address": "8.8.8.8"
}
],
"pings": [
{
"id": 1,
"createdAt": "2024-01-01T00:00:00Z",
"hostId": 1
},
{
"id": 2,
"createdAt": "2024-01-01T00:00:00Z",
"hostId": 2
}
],
"icmps": [
{
"id": 1,
"seq": 1,
"ttl": 54,
"time": 90,
"pingId": 1
},
{
"id": 2,
"seq": 2,
"ttl": 54,
"time": 95,
"pingId": 1
},
{
"id": 3,
"seq": 3,
"ttl": 54,
"time": 85,
"pingId": 1
},
{
"id": 4,
"seq": 1,
"ttl": 54,
"time": 95,
"pingId": 2
},
{
"id": 5,
"seq": 2,
"ttl": 54,
"time": 80,
"pingId": 2
},
{
"id": 6,
"seq": 3,
"ttl": 54,
"time": 99,
"pingId": 2
}
]
}
/codes/package/json-server/db.json
{
"hosts": [
{
"id": 1,
"name": "Google Search",
"address": "www.google.com"
},
{
"id": 2,
"name": "Google DNS",
"address": "8.8.8.8"
}
],
"pings": [
{
"id": 1,
"createdAt": "2024-01-01T00:00:00Z",
"hostId": 1
},
{
"id": 2,
"createdAt": "2024-01-01T00:00:00Z",
"hostId": 2
}
],
"icmps": [
{
"id": 1,
"seq": 1,
"ttl": 54,
"time": 90,
"pingId": 1
},
{
"id": 2,
"seq": 2,
"ttl": 54,
"time": 95,
"pingId": 1
},
{
"id": 3,
"seq": 3,
"ttl": 54,
"time": 85,
"pingId": 1
},
{
"id": 4,
"seq": 1,
"ttl": 54,
"time": 95,
"pingId": 2
},
{
"id": 5,
"seq": 2,
"ttl": 54,
"time": 80,
"pingId": 2
},
{
"id": 6,
"seq": 3,
"ttl": 54,
"time": 99,
"pingId": 2
}
]
}
$ npm init -y
$ npm i json-server
$ npx json-server --watch db.json
$ npm init -y
$ npm i json-server
$ npx json-server --watch db.json
Module
Arquivos
json-server
├── db.json
├── package-lock.json
├── package.json
└── src
└── server.js
Arquivos
json-server
├── db.json
├── package-lock.json
├── package.json
└── src
└── server.js
/codes/package/json-server/package.json
{
"name": "json-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "json-server --watch db.json",
"dev": "node src/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"json-server": "^0.17.3"
}
}
/codes/package/json-server/package.json
{
"name": "json-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "json-server --watch db.json",
"dev": "node src/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"json-server": "^0.17.3"
}
}
/codes/package/json-server/src/server.js
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running');
});
/codes/package/json-server/src/server.js
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running');
});
Rotas
Método | Caminho | Resposta |
---|---|---|
POST | /hosts | Cria um novo host |
GET | /hosts | Retorna todos os hosts |
GET | /hosts?id=1 | Retorna o host com id igual a 1 |
GET | /hosts/1 | Retorna o host com id igual a 1 |
PUT | /hosts/1 | Atualiza o host com id igual a 1 |
DELETE | /hosts/1 | Exclui o host com id igual a 1 |
- Respostas de informação (100-199)
- Respostas de sucesso (200-299)
- Redirecionamentos (300-399)
- Erros do cliente (400-499)
- Erros do servidor (500-599)
Exemplos de Códigos
Código | Nome | Significado |
---|---|---|
200 | Ok | Solicitação gerada com sucesso |
201 | Created | Solicitação gerada com sucesso e um novo recurso foi criado como resultado |
204 | No Content | Solicitação gerada com sucesso e não há conteúdo para ser enviado |
400 | Bad Request | Solicitação não compreendida por motivos de erro |
401 | Unauthorized | Solicitação foi bloqueada porque não possui credenciais de autenticação válidas |
404 | Not Found | O servidor não pode encontrar o recurso solicitado |
500 | Internal Server Error | O servidor encontrou uma situação com a qual não sabe lidar. |
Create a host
@server=http://localhost:3000
@createdHostId = {{createHost.response.body.$.id}}
### Create a host
# @name createHost
POST {{server}}/hosts
Content-Type: application/json
{
"name": "DNS Server",
"address": "1.1.1.1"
}
@server=http://localhost:3000
@createdHostId = {{createHost.response.body.$.id}}
### Create a host
# @name createHost
POST {{server}}/hosts
Content-Type: application/json
{
"name": "DNS Server",
"address": "1.1.1.1"
}
$ curl -X POST http://localhost:3000/hosts \
-d '{"name":"DNS Server","address":"1.1.1.1"}'
$ curl -X POST http://localhost:3000/hosts \
-d '{"name":"DNS Server","address":"1.1.1.1"}'
Read hosts
@server=http://localhost:3000
### Read hosts
GET {{server}}/hosts
@server=http://localhost:3000
### Read hosts
GET {{server}}/hosts
$ curl http://localhost:3000/hosts
$ curl http://localhost:3000/hosts
Read a host (id)
@server=http://localhost:3000
@createdHostId = {{createHost.response.body.$.id}}
### Read a host
GET {{server}}/hosts/{{createdHostId}}
@server=http://localhost:3000
@createdHostId = {{createHost.response.body.$.id}}
### Read a host
GET {{server}}/hosts/{{createdHostId}}
$ curl http://localhost:3000/hosts/3
$ curl http://localhost:3000/hosts/3
Update a host
@server=http://localhost:3000
@createdHostId = {{createHost.response.body.$.id}}
### Update a host
PUT {{server}}/hosts/{{createdHostId}}
Content-Type: application/json
{
"name": "Cloudflare DNS",
"address": "1.1.1.1"
}
@server=http://localhost:3000
@createdHostId = {{createHost.response.body.$.id}}
### Update a host
PUT {{server}}/hosts/{{createdHostId}}
Content-Type: application/json
{
"name": "Cloudflare DNS",
"address": "1.1.1.1"
}
$ curl -X PUT http://localhost:3000/hosts/3 \
-d '{"name":"Cloudflare DNS","address":"1.1.1.1"}'
$ curl -X PUT http://localhost:3000/hosts/3 \
-d '{"name":"Cloudflare DNS","address":"1.1.1.1"}'
Delete a host
@server=http://localhost:3000
@createdHostId = {{createHost.response.body.$.id}}
DELETE {{server}}/hosts/{{createdHostId}}
@server=http://localhost:3000
@createdHostId = {{createHost.response.body.$.id}}
DELETE {{server}}/hosts/{{createdHostId}}
$ curl -X DELETE http://localhost:3000/hosts/3
$ curl -X DELETE http://localhost:3000/hosts/3