Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
Node Js fs.writeFile changes existing object instead of pushing data to it

I have a simple blog website that has a submit form and the data from that is posted on the screen, and sent as an object to my node js server. Since I want all of my existing posts to stay on the website(after refresh) I need to keep my data in JSON. I have been stuck on this part all day and can't figure it out. Here is where I think the error/ misunderstanding is.

app.post('/api', (req, res) => {
    const data = req.body;
    const words = JSON.stringify(data, null, 2);
    res.send('success')
    fs.writeFile('data.json', words, finished);
    function finished(err) {
    console.log('all set')
        }
    console.log(words) 
   }
);

What I have currently adds the data to the 'data.json' file, however whenever I try to add another post on the page, the data in the JSON file only keeps the most recent submitted data. I am a new programmer so I apologize if it is something obvious. Thanks in advance and if you need more information just ask!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!