generated from skills/copilot-codespaces-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b394abd
commit a36d092
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |