-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (45 loc) · 1.46 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
const express = require("express");
const url_model = require("./models/shorturl.js");
const mongoose = require("mongoose");
const app = express();
// constant
const PORT = 5000;
const LOCALHOST_IP = "127.0.0.1";
const MONGO_URI = `mongodb://${LOCALHOST_IP}:27017/Url_Shortner`;
const connectMongoDB = async (mongoURI) => {
try {
await mongoose.connect(mongoURI);
console.log("Successfully Connected to MongoDB !!");
} catch (error) {
console.log(error);
process.exit(1);
}
};
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
// Routes
app.get("/", async (req, res) => {
const urlLists = await url_model.find();
res.render("index", { shorturls: urlLists });
});
app.post("/shorturls", async (req, res) => {
await url_model.create({ full: req.body.fullUrl });
res.redirect("/");
});
app.get("/:shorturl", async (req, res) => {
const shorturl = await url_model.findOne({ short: req.params.shorturl });
if (shorturl == null) return res.sendStatus(404);
shorturl.clicks++;
shorturl.save();
await res.redirect(shorturl.full);
});
app.post("/:id", async (req, res) => {
const id = req.params.id;
const deleteAction = await url_model.findOneAndDelete({ _id: id });
if (deleteAction == null) return res.sendStatus(404);
res.redirect("/");
});
app.listen(PORT, () => {
connectMongoDB(MONGO_URI);
console.log(`Server is running on PORT ${PORT}`);
});