// server.js

"use strict";

const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults();

const path = require("path");

const PORT = 3000;
const HOST_NAME = "localhost";
const HOST = `${HOST_NAME}:${PORT}`;

// To handle POST, PUT and PATCH you need to use a body-parser
// You can use the one used by JSON Server
server.use(jsonServer.bodyParser);

// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares);

// Use default router
server.use("/api", router);

// Add custom routes before JSON Server router
server.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, "index.html"));
})

server.listen(PORT, () => {
  console.log(`JSON Server is running on ${HOST}`);
});

// server.js

"use strict";

const jsonServer = require('json-server');
const server = jsonServer.create();
const middlewares = jsonServer.defaults();

const path = require("path");

const PORT = 3000;
const HOST_NAME = "localhost";
const HOST = `${HOST_NAME}:${PORT}`;

// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares);

// Add custom routes before JSON Server router
server.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, "index.html"));
})

server.listen(PORT, () => {
  console.log(`JSON Server is running on ${HOST}`);
});

--- 
+++ 
@@ -4,6 +4,7 @@
 
 const jsonServer = require('json-server');
 const server = jsonServer.create();
+const router = jsonServer.router('db.json')
 const middlewares = jsonServer.defaults();
 
 const path = require("path");
@@ -12,8 +13,15 @@
 const HOST_NAME = "localhost";
 const HOST = `${HOST_NAME}:${PORT}`;
 
+// To handle POST, PUT and PATCH you need to use a body-parser
+// You can use the one used by JSON Server
+server.use(jsonServer.bodyParser);
+
 // Set default middlewares (logger, static, cors and no-cache)
 server.use(middlewares);
+
+// Use default router
+server.use("/api", router);
 
 // Add custom routes before JSON Server router
 server.get('/', function (req, res) {
Add custom route to API with db.json
