This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
155 lines (130 loc) · 3.59 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const express = require("express");
const app = express();
var bodyParser = require("body-parser");
const Swal = require("sweetalert2");
const Collect = require("@supercharge/collections");
const db = require("quick.db");
require('dotenv').config()
app.use(bodyParser());
app.set("json spaces", 2);
app.use(express.static("public"));
app.set("view engine", "ejs");
function hasWhiteSpace(s) {
return /\s/.test(s);
}
var specialChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
var checkForSpecialChar = function (string) {
for (i = 0; i < specialChars.length; i++) {
if (string.indexOf(specialChars[i]) > -1) {
return true;
}
}
return false;
};
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
let counting = db.fetch(`shorted`);
setInterval(function () {
counting = db.fetch(`shorted`);
}, 5000);
/* ----- MONGODB ----- */
var monk = require("monk");
const thedb = monk(process.env.MONGODB);
thedb
.then(() => {
console.log("[ MONGODB ] Connected to mongo server.");
})
.catch((e) => {
console.log("[ MONGODB ] Failed to connect to server.");
});
const inidb = thedb.get("codenime-short");
/* ----- END MONGODB ----- */
app.get("/", async (req, res) => {
res.render("index", {
notavailable: false,
aliasnull: false,
deleted: false,
});
});
app.get("/test", async (req, res) => {
res.render("test", { isAdded: true, long: "test", alias: "testing", id: makeid(8) });
});
app.get("/delete", async (req, res) => {
res.render("remove", { aliasnull: false });
});
app.get("/:short", async (req, res) => {
const queryshort = req.params.short;
const final = inidb.findOne({ shorted: req.params.short }).then((docs) => {
if (docs == null) res.status(404).send("404");
else res.redirect(docs.url);
});
});
app.post("/short", async (req, res) => {
var urlnya = req.body.url;
var shortnya = req.body.short;
const inisudahada = await inidb.findOne({ shorted: shortnya });
var iddelete = makeid(8)
if (inisudahada) {
res.render("index", {
notavailable: true,
aliasnull: false,
deleted: false,
});
} else if (hasWhiteSpace(shortnya) === true) {
res.render("index", {
notavailable: true,
aliasnull: false,
deleted: false,
});
} else if (checkForSpecialChar(shortnya) === true) {
res.render("index", {
notavailable: true,
aliasnull: false,
deleted: false,
});
} else {
const pushtodb = {
url: urlnya,
shorted: shortnya,
id: iddelete
};
inidb.insert(pushtodb),
res.render("test", { isAdded: true, long: urlnya, alias: shortnya, id: iddelete }),
db.add("shorted", 1);
}
});
app.post("/remove", async (req, res) => {
var codeshort = req.body.shortcode;
var removeid = req.body.removeid;
const check = await inidb.findOne({ id: removeid });
if (check == null) {
res.render("remove", { aliasnull: true });
} else {
inidb.findOneAndDelete({ id: removeid }),
res.render("index", {
deleted: true,
aliasnull: false,
notavailable: false,
});
}
});
/*app.get('/count', async (req, res) => {
res.status(200).json({
data: {
count: {
shorted: counting
}
}
})
})*/
const listener = app.listen(3000, () => {
console.log("[ EXPRESS ] Your app listen on port " + listener.address().port);
});