-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (69 loc) · 1.8 KB
/
index.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
75
76
77
78
const express = require("express");
const app = express();
const path = require("path");
const methodOverride = require("method-override");
require("dotenv").config();
const bodyParser = require("body-parser");
const { Deta } = require("deta");
const deta = Deta(process.env.DETA);
const db = deta.Base("shortener");
const PORT = 8080;
const date = new Date();
const datetime =
"Created: " +
date.getDate() +
"/" +
(date.getMonth() + 1) +
"/" +
date.getFullYear() +
" at " +
date.getHours() +
":" +
date.getMinutes() +
":" +
date.getSeconds();
app.use(methodOverride("_method"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.post("/", async (req, res) => {
const databaseFetch = await db.fetch({ shortlink: req.body.short });
if (databaseFetch.count === 0) {
db.put({
longlink: req.body.long,
shortlink: req.body.short,
date: datetime,
});
res.redirect("/");
} else {
res.send(` <p>This link has already been used!</p>
<a href="/">naspäť</a>`);
}
});
app.get("/", (req, res) => {
res.render("index");
});
app.get("/link/:id", async (req, res) => {
const { id } = req.params;
const user = await db.fetch({ shortlink: id });
console.log(user);
if (user.count === 0) {
res.send(` <p>This link doesn't exist!</p>
<a href="/">Go back</a>`);
} else {
console.log(user);
res.redirect(user.items[0].longlink);
}
});
// Place to edit the links
app.get("/edit", async (req, res) => {
const linksList = await db.fetch({});
res.render("edit", { results: linksList.items });
});
app.delete("/edit/:id", async (req, res) => {
const { id } = req.params;
await db.delete(id);
res.redirect("/edit");
});
app.listen(PORT);
module.exports = app;