node.js - POST request with updating json file using create-react-app -
i'm making project using create-react-app. there configured server , on. i'm using react-router-dom routing in app. there 'comments' component. when starts render goes local json file , takes comments there using ajax. when user clicks 'submit' sends post request form's fields same json file. have code adding new object json file. should work when user in '/api/comments' route . code adding new object json file (requires express):
`app.post('/api/comments', function(req, res) { fs.readfile(comments_file, function(err, data) { if (err) { console.error(err); process.exit(1); } var comments = json.parse(data); var newcomment = { id: date.now(), author: req.body.author, text: req.body.text, }; comments.push(newcomment); fs.writefile(comments_file, json.stringify(comments, null, 4), function(err) { if (err) { console.error(err); process.exit(1); } res.json(comments); }); }); });`
but don't know shoud put code if i'm using 'create-react-app' , uses it's own configured server (as far know). maybe there way change server 'create-react-app' uses , put there code handle route? or maybe there way handle route using 'react-router'?
if understand question correctly code have posted here server side code. app have made using create-react-app front end application , therefore not have server side code. host second server expose api routes need , call server using http library axios.
Comments
Post a Comment