'Accessing json-server db from Custom Route middeware

I would like to know if there is a better way to access data from the json-server .json file from the json-server middleware file.

The only way i got this working was very scruffy and included starting two services like so:

  1. Starting the middleware in node node middleware.js,
  2. Starting the db.json using json-server --watch db.json --port 3001,
  3. using axios/requests in the middleware file to initiate a request for the route i want to access in the db.json file, like :

const ax = require("axios");

// for login
server.post("/login", (req, res, next) => {
  ax.get("http://localhost:3001/users", {params: {username: req.body.uName, password: req.body.password}}).then(acc => { 
    if (acc.length == 1) {
      console.log("User Found!");
      res.send("Buya")
    } else {
      res.send("Cannot log in")
    }
  });
}

This just seems like overkill for such small rewards.

I thought maybe one can probably do something like :

if(router.get("/users").render().filter(a => a.username == req.body.uName && a.password == req.body.pWord).length == 1)

The code above is not from the documentation, it's merely to give you an idea of what it is i am asking.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source