-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
32 lines (25 loc) · 994 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const fs = require('fs');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const db = low(new FileSync('db.json'));
db.defaults({ comments: [] }).write();
app.get('/', (req, res) => {
const header = fs.readFileSync('content/header.html', 'utf8');
const footer = fs.readFileSync('content/footer.html', 'utf8');
const comments = db.get('comments').value();
let content = '';
for (var i = 0; i < comments.length; i++) {
content += `<div class="commentbox"><p class="name">${"Name: " + comments[i].name}</p><p class="comment">${comments[i].text}</p><br></div>`;
}
res.send(header + content + footer);
});
app.post('/post_comment', (req, res) => {
db.get('comments').push(req.body).write();
res.redirect('/');
});
app.listen(3000);