-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
122 lines (105 loc) · 2.86 KB
/
app.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
require("dotenv").config(); //.env variables
const app = express();
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.set("view engine", "ejs");
app.use(express.static("public"));
//============DB START=====================
const uri = process.env.uri;
mongoose.connect(
uri,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
function (err) {
if (err) console.log(err);
console.log("DB connected.");
}
);
const schema = new mongoose.Schema({
url: String,
customName: String,
});
const user = mongoose.model("user", schema); //collection
const saveToDB = async (userURL, userCustomName) => {
console.log("SAVING TO DB....");
const userData = new user({
url: userURL,
customName: userCustomName,
});
await userData.save();
console.log("SAVED TO DB SUCCESFULLY!");
};
//check in DB, if not found then save to DB,return 1 otherwise return 1 if found!
const checkNameInDB = async (userURL, userCustomName) => {
console.log("CHECKING IN DB....");
const data = await user.find({
customName: userCustomName,
});
if (data.length == 1) return 1; //found
//not found
await saveToDB(userURL, userCustomName);
return 0;
};
//get link from DB if found, otherwise return home link
const getLinkFromDB = async (name) => {
console.log("Getting link from DB for name: " + name);
const data = await user.find({
customName: name,
});
// console.log("DATA",data);
if (data.length !== 0)
//found
return data[0].url;
else return herokuLink;
};
const herokuLink = "https://link-maker-pvx.onrender.com"; //home link of website!
const regex = /[^A-Za-z1-9]/g; //regex to have only alphabets and numbers
app.get("/", (req, res) => {
res.render("index", {
userURL: "",
userCustomName: "",
custom_link: " ",
});
});
app.post("/", (req, res) => {
let userURL = req.body.inputURL;
let userCustomName = req.body.inputCustomName;
let resultCustomURL = "";
if (userCustomName.match(regex) != null)
//user entered inproper custom name
res.redirect("/");
else {
//check in DB
const checkinDBPromise = checkNameInDB(userURL, userCustomName);
checkinDBPromise.then((response) => {
if (response === 0) {
//not present in DB
resultCustomURL = herokuLink + "/" + userCustomName;
}
res.render("index", {
userURL: userURL,
userCustomName: userCustomName,
custom_link: resultCustomURL,
});
});
}
});
app.get("/:name", (req, res) => {
const name = req.params.name;
const getLinkPromise = getLinkFromDB(name);
getLinkPromise.then((response) => {
console.log("Redirecting to " + response);
res.redirect(response);
});
});
app.listen(process.env.PORT || 80, () => {
console.log("Server started!");
});