'Profile management: Node js and Express

Please, I need to create a back-end application using Node js and Express framework to get post and modify user data from a json file. To tell the truth I dont know how to managed the JSON file and I was searching for information but i still having mistakes so i need help with this app, I have been tried with some code that I post below.

The functionalities are the following:

  1. "/add" -> POST Method -> This route should add the data sent with the request body into the post.json file

If the data is added succesfully, then you should sent a respone code of 200

That is why i have until now.

app.post("/add", function(req, resp){

 var jsonObject = req.body;
 var jsonFile = fs.readFileSync("post.json", "UTF8");
 var jsonArray = JSON.parse(jsonFile);
 jsonArray.push(jsonObject);

 jsonFile = JSON.stringify(jsonArray);
 resp.json(jsonFile);
 fs.writeFileSync("post.json",jsonFile,"utf-8");

});
  1. "/view" -> GET Method -> This route has a optional query param called id. If no query param get passed with the request then you should fetch all the data from the get.json file. If some id gets passed with the request query param, then you should send the details of the user having id equal to the id that comes with the request query

if the data is fetched succesfully, then you should send a response code of 200

That is why I have until now....

var express = require('express');
var url = require('url');
var app = express();
var fs = require('fs');

app.get('/view', function(req, resp){
  var queryURL = url.parse(req.url, true).query;
  var jsonFile = fs.readFileSync("post.json", "UTF8");
  var data = JSON.parse(jsonFile);

  if(typeof queryURL.id === "undefined" || queryURL.id == 0){
    resp.json(data);
  }else{
    resp.json(data[queryURL.id-1]);
  }

});

app.listen(3000);
  1. /edit/:id -> PATCH Method -> This route should update the values if the user having id in post.json equal to the id that comes with the request URL. You can update name, age and email of users.

If the data is updated succesfully, then you should send a status code of 200

In this case i dont know how to proceed!!!



Solution 1:[1]

Try the following code for the thrid scenario.

3. /edit/:id -> PATCH Method -> This route should update the values if the user having id in post.json equal to the id that comes with the request URL. You can update name, age and email of users. If the data is updated succesfully, then you should send a status code of 200.

app.patch("/edit/:id", function(req, res){
    let userID = req.params.id;
    let userFile = fs.readFileSync("post.json", "UTF-8");
    let userArray = JSON.parse(userFile);
    
    let reqUserObject = req.body;

    let newUserArray = userArray.map(user => {
        if (user.id === userID) {
            updatedUser = {...user, ...reqUserObject};
            return updatedUser;
        } else {
            return user;
        }
    });
    
    userFileData = JSON.stringify(newUserArray);
    res.json(userFileData);
    fs.writeFileSync("post.json", userFileData, "UTF-8");
});

Sources

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

Source: Stack Overflow

Solution Source
Solution 1