Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda