Skip to content

Commit

Permalink
Added web server
Browse files Browse the repository at this point in the history
  • Loading branch information
dferencz-laird committed Jun 10, 2024
1 parent ed72ee9 commit 7e65a94
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Create web server and listen to port 3000
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const fs = require('fs');

app.use(bodyParser.json());

// Read comments from file
const comments = JSON.parse(fs.readFileSync('comments.json'));

app.get('/comments', (req, res) => {
res.json(comments);
});

app.post('/comments', (req, res) => {
comments.push(req.body);
fs.writeFileSync('comments.json', JSON.stringify(comments, null, 2));
res.status(201).json(req.body);
});

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

0 comments on commit 7e65a94

Please sign in to comment.