Skip to content

Commit 78c36e3

Browse files
committed
[node] Use express's built-in json headers, set no cache on GET
1 parent fef6397 commit 78c36e3

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

server.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ app.use(bodyParser.urlencoded({extended: true}));
2424

2525
app.get('/comments.json', function(req, res) {
2626
fs.readFile('comments.json', function(err, data) {
27-
res.setHeader('Content-Type', 'application/json');
28-
res.send(data);
27+
res.setHeader('Cache-Control', 'no-cache');
28+
res.json(JSON.parse(data));
2929
});
3030
});
3131

@@ -34,9 +34,8 @@ app.post('/comments.json', function(req, res) {
3434
var comments = JSON.parse(data);
3535
comments.push(req.body);
3636
fs.writeFile('comments.json', JSON.stringify(comments, null, 4), function(err) {
37-
res.setHeader('Content-Type', 'application/json');
3837
res.setHeader('Cache-Control', 'no-cache');
39-
res.send(JSON.stringify(comments));
38+
res.json(comments);
4039
});
4140
});
4241
});

0 commit comments

Comments
 (0)