Skip to content

Commit 4c25606

Browse files
committed
Copilot third commit
1 parent 3ae6ba5 commit 4c25606

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

comments.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Create web server
2+
//
3+
var express = require('express');
4+
var app = express();
5+
var bodyParser = require('body-parser');
6+
var fs = require('fs');
7+
var path = require('path');
8+
9+
app.use(bodyParser.urlencoded({ extended: false }));
10+
app.use(bodyParser.json());
11+
12+
// Read comments from file
13+
app.get('/comments', function(req, res) {
14+
fs.readFile('comments.json', 'utf8', function(err, data) {
15+
if (err) {
16+
console.log('Error: ' + err);
17+
res.send('Error: ' + err);
18+
return;
19+
}
20+
res.send(data);
21+
});
22+
});
23+
24+
// Write comments to file
25+
app.post('/comments', function(req, res) {
26+
fs.readFile('comments.json', 'utf8', function(err, data) {
27+
if (err) {
28+
console.log('Error: ' + err);
29+
res.send('Error: ' + err);
30+
return;
31+
}
32+
var comments = JSON.parse(data);
33+
comments.push(req.body);
34+
fs.writeFile('comments.json', JSON.stringify(comments, null, 4), function(err) {
35+
if (err) {
36+
console.log('Error: ' + err);
37+
res.send('Error: ' + err);
38+
return;
39+
}
40+
res.send(comments);
41+
});
42+
});
43+
});
44+
45+
// Start server
46+
var server = app.listen(3000, function() {
47+
console.log('Server listening on port 3000');
48+
});

0 commit comments

Comments
 (0)