0

This is my JSON file:

 [
  {
    "id": 1,
    "title": "React Game!",
    "description": "Minesweeper game created using React",
    "URL": "https: //react-game-two.herokuapp.com/"
  },
  {
    "id": 2,
    "title": "Online store",
    "description": "Online store created with HTML, CSS and JavaScript.",
    "URL": "https: //git.com/myrepos/shop/index"
  }
]

This is my app.js:

const express = require('express')
const app = express()
const fs = require('fs')
const PORT = process.env.PORT || 3000;
const data = require('./webProjects.json')

app.get('/', (req, res) => {
  res.send(data)
})

app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
})

app.post('/', (req, res) => {
  fs.appendFile('webProjects.json', [`{"id": "${data.length + 1}", "title": "${req.query.length}"}`], (err) => {
    if (err) throw err;
    res.send('File created!');
  });
})

The app.post() code kind of works. It adds the object but adds it outside of the array, in the JSON file, and i'm trying to append it to the JSON array, inside the square brackets.

2
  • 2
    Does this answer your question? stackoverflow.com/a/36856787/12397250 (number 5) Commented Jul 10, 2020 at 19:05
  • @Owl thank you, will have a look Commented Jul 10, 2020 at 19:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.