0

I am trying to update a local JSON file with a button click to update the state of the button. Currently i am able to get the JSON file and read all button state. below am pasting my button_status.json file.

[
{
  "id": "0x11",
  "name": "cam1",
  "state": true
},
{
  "id": "0x12",
  "name": "cam2",
  "state": true
},
{
  "id": "0x13",
  "name": "cam3",
  "state": false
},
{
  "id": "0x14",
  "name": "cam4",
  "state": false
},
{
  "id": "0x15",
  "name": "cam5",
  "state": false
},
{
  "id": "0x111",
  "name": "cam6",
  "state": false
}]

i am using express nodejs server for reading the button_status.json file. below am adding my express get function.

app.get('/getButtons', function (req, res) {
fs.readFile( __dirname + "/" + "button_status.json", 'utf8', function (err, data) {
   console.log( data );
   res.end( data );
});
});

Angular service code to call the get method.

getButtonStatus() {
return this.http.get('http://localhost:3000/api/getButtons')
             .map(res => res.json())
             .concatMap(res => res)
             .filter((x:any) => x.state === true)
             .do(res=> console.log("res",res))
}

with the below code i am removing the hidden property for which the button state is true.

this.buttonStatus = _mcxAppLayoutService.getButtonStatus()
.subscribe(value=>{
  $('#'+value.name).removeAttr('hidden');
});

Now i want to change the button state to true/false in the JSON file when anyone want to change using some button click event. how can i create a put request to update the state of each button. can anyone help me to do this. thanks for your help in advance.

Nidhin
  • 167
  • 1
  • 5
  • 26
  • Check [this post](http://stackoverflow.com/questions/10685998/how-to-update-a-value-in-a-json-file-and-save-it-through-node-js). I think could help you – Elmer Dantas Apr 03 '17 at 09:46
  • @ElmerDantas is there any possible way of creating PUT request in express to do this task – Nidhin Apr 03 '17 at 10:11
  • yeah, it's possible. Unfortunately I cannot help you with this solution now. I'll let some links that might help you apart from the one I already posted: 1 - [Express PUT](https://expressjs.com/en/starter/basic-routing.html). 2 - [perform put from client](http://stackoverflow.com/questions/38539094/how-to-make-a-put-request-in-angular2-using-http). Hope it helps. – Elmer Dantas Apr 03 '17 at 11:00
  • @ElmerDantas thank you for the quick reply. i am really stuck with task. the links which you send me is the part which is from the angular 2. this part is already done. i want to handle this put request in nodejs with express. it will be a grate help if anyone knows and ready to help me – Nidhin Apr 03 '17 at 11:43
  • the first link is how to create an route using express to handle your `PUT`...you need to do something like: pass the object you're change as parameter...inside the `app.put`, you'll read the `button_status.json` (like you already did) and convert the `data` to JSON using `var obj = JSON.parse(data);` so that you'll have `obj` array that you can iterate, find the object with the same `id` you pass as parameter, update it and write the `obj` into the file you've read using `fs.writeFile`...do you get the idea? – Elmer Dantas Apr 03 '17 at 12:34

0 Answers0