-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (50 loc) · 1.76 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const app = express();
const port = 3000
const path = require("path")
//let comments = require("/srv/www/htdocs/blog/comments.js")
const fs = require("fs")
const validator = require('validator');
const rootPath = "/srv/www/htdocs/"
const escapeHtml = (unsafe) => {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};
// Middleware to parse URL-encoded and JSON bodies
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.post("/blog/submit-comment",(req,res) => {
let jsonString = fs.readFileSync('/srv/www/htdocs/blog/comments-database.js', 'utf8');
let comments = JSON.parse(jsonString);
res.redirect(302, req.get("referer"));
console.log(req.get("referer"))
console.log(req.body.name)
if (req.body.name != "" && req.body.comment !="") {
comments[req.body.pageID].push([validator.escape(escapeHtml(req.body.name)),validator.escape(escapeHtml(req.body.comment))])
content=`comments=${JSON.stringify(comments)}
if (typeof module !== "undefined" && module.exports) {
module.exports = comments;
}`
fs.writeFile("/srv/www/htdocs/blog/comments.js", content, (err) => {
if (err) {
console.error('Error creating file:', err);
} else {
console.log('File created written: Comments.js');
}
});
const jsonString = JSON.stringify(comments);
fs.writeFileSync('/srv/www/htdocs/blog/comments-database.js', jsonString, 'utf8');
}
console.log(comments)
});
app.use(express.static(rootPath));
// Start the server
app.listen(port, () => {
console.log(`Server is running at
http://localhost:${port}
in directory: ${rootPath}`);
});