Skip to content

Commit

Permalink
Copilot third commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenskrause committed May 3, 2024
1 parent b394abd commit a36d092
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Create web server with express
const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');

// Use the static files from the public folder
app.use(express.static('public'));

// Read the comments from the file
app.get('/comments', (req, res) => {
fs.readFile(path.join(__dirname, 'comments.json'), (err, data) => {
if (err) {
res.status(500).send('Unable to read the comments file');
} else {
res.send(data);
}
});
});

// Add a new comment to the file
app.post('/comments', express.json(), (req, res) => {
fs.readFile(path.join(__dirname, 'comments.json'), (err, data) => {
if (err) {
res.status(500).send('Unable to read the comments file');
} else {
const comments = JSON.parse(data);
comments.push(req.body);
fs.writeFile(path.join(__dirname, 'comments.json'), JSON.stringify(comments), (err) => {
if (err) {
res.status(500).send('Unable to write the comments file');
} else {
res.send('Comment added');
}
});
}
});
});

// Listen on port 3000
app.listen(3000, () => {
console.log('Server listening on port 3000');
});

0 comments on commit a36d092

Please sign in to comment.