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.

begdev
  • 35
  • 8

0 Answers0