How do I update data in my JSON file using Postman?

39 Views Asked by At

How can I update my data in JSON file using patch method in postman? I want when I select the patch method in postman and I click on send my data should be updated in JSON file but I don't know the syntax.

const express = require("express");
const fs = require("fs");
const users = require("./MOCK_DATA.json");
const app = express();
const PORT = 8000;
app.use(express.urlencoded({ extended: false }));

app.route("/api/users:id").get((req, res) => {
    const id = Number(req.params.id);
    const user = users.find((user) => user.id === id);
    return res.json(user);
  })
.patch((req, res) => {
    return res.json({ status: "Pending" });
  })
.delete((req, res) => {
 return res.json({ status: "Pending" });
  });

app.post("/api/users", (req, res) => {
  const body = req.body;
  users.push({ ...body, id: users.length + 1 });
  fs.writeFile("./MOCK_DATA.json", JSON.stringify(users), (err, data) => {
    try {
      return res.json({ status: "Success", id: users.length + 1 });
    } catch (err) {
      console.log(`OOPS! something went worng :( ${err}`);
    }
  });
});
app.listen(PORT, () => console.log(`Port started at ${PORT}`));
1

There are 1 best solutions below

2
AKHIL EM On

Using a JSON file for storing data is not recommended. However, if you still want to use a JSON file, you can use this npm package. lowdb