Skip to content

Commit b24db60

Browse files
committed
Merge pull request reactjs#17 from ASwitlyk/master
2 parents 7d31d05 + 3d2d078 commit b24db60

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

server.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@ var express = require('express');
1616
var bodyParser = require('body-parser');
1717
var app = express();
1818

19-
var comments = JSON.parse(fs.readFileSync('_comments.json'));
20-
2119
app.use('/', express.static(path.join(__dirname, 'public')));
2220
app.use(bodyParser.json());
2321
app.use(bodyParser.urlencoded({extended: true}));
2422

2523
app.get('/comments.json', function(req, res) {
26-
res.setHeader('Content-Type', 'application/json');
27-
res.send(JSON.stringify(comments));
24+
fs.readFile('_comments.json', function(err, data) {
25+
res.setHeader('Content-Type', 'application/json');
26+
res.send(data);
27+
});
2828
});
2929

3030
app.post('/comments.json', function(req, res) {
31-
comments.push(req.body);
32-
fs.writeFile('_comments.json', JSON.stringify(comments))
33-
res.setHeader('Content-Type', 'application/json');
34-
res.send(JSON.stringify(comments));
31+
fs.readFile('_comments.json', function(err, data) {
32+
var tempComments = JSON.parse(data);
33+
tempComments.push(req.body);
34+
fs.writeFile('_comments.json', JSON.stringify(tempComments), function(err) {
35+
res.setHeader('Content-Type', 'application/json');
36+
res.send(JSON.stringify(tempComments));
37+
});
38+
});
3539
});
3640

3741
app.listen(3000);

0 commit comments

Comments
 (0)