I want to get multiple usernames and surnames with the form and store them all in a single user.json file.
const express = require('express')
const app = express()
const fs = require('fs')
const bodyparser = require('body-parser')
app.use(bodyparser.urlencoded({extended: false}))
app.use(bodyparser.json())
app.use(express.static('./public'))
app.post('/', (req, res, next)=> {
let form = {
firstname: req.body.firstname,
lastname: req.body.lastname
}
let jsonObject = JSON.stringify(form, null , 2)
fs.appendFileSync('user.json', jsonObject)
next()
})
app.listen(8080, ()=> {
console.log('Server running ...');
})
When I submit multiple submissions with AppendFile() I get the following output
[
]
{
"firstname": "test",
"lastname": "test"
}
I want to save multiple data in a single json file