-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (51 loc) · 1.45 KB
/
server.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
56
57
58
59
60
61
62
const express = require("express")
const app = express()
const port = 3000
const path = require('path')
const sqlite3 = require("sqlite3").verbose()
const db_comment = new sqlite3.Database("./topic_comments_data.db")
const db_topic = new sqlite3.Database("./topic_content_data.db")
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Content-Type')
res.header('Access-Control-Allow-Methods', '*')
next();
});
app.use('/', express.static(path.join(__dirname, 'frontend')))
app.get("/comments/:uid", (req, res) => {
let uid = req.params.uid;
if (uid.length > 16) {
res.send(406);
return;
} else {
let sql = `SELECT * FROM Statistic WHERE author_uid = '${uid}' and reply_time > '2022-12-31 11:21:00' and reply_time < '2023-04-18 00:00:00'`;
db_comment.all(sql, (err, rows) => {
if (err) {
console.error(err);
res.json([]);
return;
}
res.json(rows);
});
}
});
app.get("/topics/:uid", (req, res) => {
let uid = req.params.uid;
if (uid.length > 16) {
res.send(406);
return;
} else {
let sql = `SELECT * FROM Topic WHERE author_uid = '${uid}'`;
db_topic.all(sql, (err, rows) => {
if (err) {
console.error(err);
res.json([]);
return;
}
res.json(rows);
});
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});