Jhead45
4/24/2018 - 9:58 PM

saving form submissions to JSON

Paths must be defined. BodyParser is used to allow form data to be read. The app posts (writeFile to JSON File). (using appendFile instead of writeFile will form a moving log)

const express = require('express');
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
let app = express();

const clientPath = path.join(__dirname, '../public');
const formSubmit = path.join(__dirname, '../formsubmisisons.json');

app.use(bodyParser.urlencoded({ extended: false }));


app.use(express.static(clientPath));                        

app.post('/formsubmissions', (req, res) => {                
    console.log(req.body);
    let formValue = JSON.stringify(req.body)
    fs.writeFile(formSubmit, formValue, (err) => {
        if (err) {console.log(err);} 
   })
    res.send('Thanks for submitting!');
});



app.listen(3000, () => {                                    
    console.log('listening to port 3000');
});