Save an SQL object to a file using Node.js

Viewed 90

I need to save the object rows in a file or save it to a JSON file.

app.get('/getposts', (req, res) => {
  mysqlConnection.query('Select * from posts', (err, rows, fields) => {
    if (!err) console.log(rows);
    else
      console.log(err);

    res.send(rows);
1 Answers
fs.writeFile('filename.json', JSON.stringify(rows), err => err && console.log(err));

This will stringify rows and write it to filename.json.

Related