-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (44 loc) · 1.43 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
const express = require("express");
const path = require("path");
const { nanoid } = require("nanoid");
const app = express();
const views = path.join(__dirname, "./views");
const PORT = process.env.PORT || 3000;
require("./db");
const ShortUrl = require("./models/shortUrl");
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
app.use(express.static(views));
app.get("/", async (req, res) => {
const shortUrls = await ShortUrl.find();
res.render("index", { shortUrls: shortUrls });
});
app.post("/shortenUrl", async (req, res) => {
const URL = req.body.url;
const ID = req.body.id;
if (!URL) return res.send("Please enter a URL");
// Check if ID is already in use
if (ID) {
if (await ShortUrl.findOne({ short: ID })) {
return res.send("ID is already in use");
}
}
ShortUrl.create({ full: URL, short: ID || nanoid(3) }, (err, data) => {
if (err) {
console.log("🦄 Error:", err);
} else {
console.log("✅ URL sent to DB:", data);
res.status(200).redirect("/");
}
});
});
app.get("/:shortUrl", async (req, res) => {
const shortUrl = await ShortUrl.findOne({ short: req.params.shortUrl });
if (!shortUrl) return res.status(404).send('URL not found. <a href="/">Go Back</a>');
shortUrl.clicks++;
shortUrl.save();
res.redirect(shortUrl.full);
});
app.listen(PORT, () => {
console.log(`🟢 Server listening on: ▶️ http://localhost:${PORT}`);
});