I'm trying to create a very simple web app where users can create an event on a web calendar to be displayed. The calendar application I am using has the capability of reading a json file to get event information. I've gotten the calendar component working, and the api is set up to send data properly. Im having issues trying to process the data when it hits post portion of my api. I've narrowed it down particularly to my fs.writefile. I seem to be doing something wrong here. The rest of the code base works fine and when I comment out the section where I attempt to write the post data being sent to /createEvent The whole code works(minus the intended goal of writing to my json file)
Heres the error I get:
(node:12756) [DEP0018] DeprecationWarning: Unhandled promise rejections are `deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.`
Here is an example post of what my API is sending, but failing to process:
{ userInput: { title: 'Vinny', start: '2018-12-01', end: '2018-12-01' } }
Heres my code:
const express = require('express');
const routes = require('./routes/');
const app = express();
const port = 3000;
const router = express.Router();
const cors = require('cors');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const fs = require('fs')
import ('./calendarevents.json');
routes(router);
app.use(cors()); // Allows for Cross origin requests
app.use(bodyParser.json()) // Turns all post requests to json
app.use(bodyParser.urlencoded({ extended: false }));
app.use(helmet()); // Helps secure apps with Http headers
app.use('/api',router);
app.get('/', (req, res) => res.send('Hello World!'))
app.use(bodyParser.json())
app.get('/api/createEvent'), (req, res) => {
res.render(req.body)
}
app.post('/api/createEvent', (req, res) => {
console.log(req.body);
//res.send("recieved your request!")
const UserInput = JSON.stringify(req.body);
fs.writeFile('./calendarevents.json', UserInput, (err) => {
if (err) throw err;
console.log("The file was saved!");
});
});
app.listen(port, () => console.log(`StoreUI Backend listening on ${port}!`))