Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda