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
    }
  ]
}
 

json-server

$ 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

Open in GitHub

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étodoCaminhoResposta
POST/hostsCria um novo host
GET/hostsRetorna todos os hosts
GET/hosts?id=1Retorna o host com id igual a 1
GET/hosts/1Retorna o host com id igual a 1
PUT/hosts/1Atualiza o host com id igual a 1
DELETE/hosts/1Exclui o host com id igual a 1

HTTP Status Codes

Exemplos de Códigos

CódigoNomeSignificado
200OkSolicitação gerada com sucesso
201CreatedSolicitação gerada com sucesso e um novo recurso foi criado como resultado
204No ContentSolicitação gerada com sucesso e não há conteúdo para ser enviado
400Bad RequestSolicitação não compreendida por motivos de erro
401UnauthorizedSolicitação foi bloqueada porque não possui credenciais de autenticação válidas
404Not FoundO servidor não pode encontrar o recurso solicitado
500Internal Server ErrorO 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

Editar esta página