Skip to content

Commit

Permalink
Added error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaudgalya committed Nov 2, 2015
1 parent 55e01a9 commit 2ddef37
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ app.use(bodyParser.urlencoded({extended: true}));

app.get('/api/comments', function(req, res) {
fs.readFile(COMMENTS_FILE, function(err, data) {
if (err) console.log(err);
res.setHeader('Cache-Control', 'no-cache');
res.json(JSON.parse(data));
});
});

app.post('/api/comments', function(req, res) {
fs.readFile(COMMENTS_FILE, function(err, data) {
if (err) console.log(err);
var comments = JSON.parse(data);
comments.push(req.body);
fs.writeFile(COMMENTS_FILE, JSON.stringify(comments, null, 4), function(err) {
if (err) console.log(err);
res.setHeader('Cache-Control', 'no-cache');
res.json(comments);
});
Expand Down

0 comments on commit 2ddef37

Please sign in to comment.