-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (64 loc) · 1.97 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
63
64
65
66
67
68
69
70
71
72
73
74
let r = require("rethinkdb");
let { server, rethinkdb } = require("./config.json");
let express = require("express");
let app = express();
let db = require("./db/");
app.set("view engine", "ejs");
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(`${__dirname}/views/index.html`);
});
app.get("/add", (req, res) => {
res.sendFile(`${__dirname}/views/addFile.html`);
});
app.get("/api/add/:title", async (req, res) => {
let title = req.params["title"]; // /api/add/<title>
let code = req.query["code"]; // /api/add/<title>?code=<code>
if (code.length < 3 || title.length < 2) {
return res.redirect("/");
}
let length = await r
.table(rethinkdb.table)
.count()
.run(global.conn);
db.addFile({ code: code, title: title, id: length + 1 });
res.redirect(`/file/${length + 1}`);
});
app.get("/api/file/:id", async (req, res) => {
let id = req.params.id;
let file = await r
.table(rethinkdb.table)
.filter({ id: parseInt(id) })
.coerceTo("array")
.run(global.conn);
if (!file[0]) return res.redirect("/");
let code = file[0]["code"];
let title = file[0]["title"];
res.json({ title: title, code: code.replace(/<br\/>/gm, "\n") });
});
app.get("/file/:id", async (req, res) => {
let id = req.params.id;
let file = await r
.table(rethinkdb.table)
.filter({ id: parseInt(id) })
.coerceTo("array")
.run(global.conn);
if (!file[0]) return res.redirect("/");
let code = file[0]["code"];
let title = file[0]["title"];
res.render(`${__dirname}/views/viewFile.ejs`, { title: title, code: code });
});
app.get("/info", async (req, res) => {
let length = await r
.table(rethinkdb.table)
.count()
.run(global.conn);
res.render(`${__dirname}/views/info.ejs`, { length: length });
});
app.get("*", (req, res) => {
return res.redirect("/");
});
app.listen(server.port, () => {
console.log(`Server Ready!\nPort: ${server.port}`);
db.init({ host: rethinkdb.host, port: rethinkdb.port });
});