JSON Server

CLI

/codes/package/json-server/db.json
{
  "investments": [
    {
      "id": 1,
      "name": "Tesouro Selic 2029",
      "value": "100000"
    },
    {
      "id": 2,
      "name": "Tesouro IPCA 2029",
      "value": "10000"
    }
  ]
}
 
/codes/package/json-server/db.json
{
  "investments": [
    {
      "id": 1,
      "name": "Tesouro Selic 2029",
      "value": "100000"
    },
    {
      "id": 2,
      "name": "Tesouro IPCA 2029",
      "value": "10000"
    }
  ]
}
 

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

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/investmentsCria um novo investimento
GET/investmentsRetorna todos os investimentos
GET/investments?id=1Retorna o investimento com id igual a 1
GET/investments/1Retorna o investimento com id igual a 1
PATCH/investments/1Atualiza o investimento com id igual a 1
DELETE/investments/1Exclui o investimento com id igual a 1

Editar esta página