Skip to content

Commit

Permalink
Merge pull request reactjs#95 from vmaudgalya/master
Browse files Browse the repository at this point in the history
Added error handling
  • Loading branch information
zpao committed Nov 12, 2015
2 parents be3fa27 + 84129a5 commit a45fe12
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,28 @@ app.use(bodyParser.urlencoded({extended: true}));

app.get('/api/comments', function(req, res) {
fs.readFile(COMMENTS_FILE, function(err, data) {
if (err) {
console.error(err);
process.exit(1);
}
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.error(err);
process.exit(1);
}
var comments = JSON.parse(data);
comments.push(req.body);
fs.writeFile(COMMENTS_FILE, JSON.stringify(comments, null, 4), function(err) {
if (err) {
console.error(err);
process.exit(1);
}
res.setHeader('Cache-Control', 'no-cache');
res.json(comments);
});
Expand Down

0 comments on commit a45fe12

Please sign in to comment.