-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
209 lines (189 loc) · 6.03 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import express from "express";
import { nanoid } from "nanoid"; // produce random URL-friendly key
import { pool1, getOriginalURL1 } from "./database/mysql-1.js";
import { pool2, getOriginalURL2 } from "./database/mysql-2.js";
import { pool3, getOriginalURL3 } from "./database/mysql-3.js";
import { redisClient, checkRedisConnected } from "./cache/redis.js";
import { checkCache } from "./middleware/checkCache.js";
// import { client, db } from "./database/mongodb.js";
const app = express();
app.use(express.urlencoded());
app.use(express.json());
app.use("/public", express.static("public"));
app.set("view engine", "ejs");
app.set("views", "./views");
const host_ip = "ctceth.com";
// test
// app.get("/", (req, res) => {
// const randomKey = nanoid(5);
// const lastkey = randomKey[randomKey.length - 1];
// const key = randomKey + "1";
// console.log(randomKey);
// console.log(lastkey);
// console.log(key);
// res.send(randomKey);
// });
app.get("/", (req, res) => {
res.render("short-url");
});
// POST API for creating short URL and checking collision
app.post("/", async (req, res) => {
const originalURL = req.body.originalUrl;
console.log(originalURL);
const randomKey = nanoid(5);
const lastNumber = "1";
let finalKey = randomKey + lastNumber;
const connection = await pool1.getConnection();
try {
await connection.beginTransaction();
let result = "";
// connect specified DB to check whether finalKey already exist
const responseCheckFinalKey = await connection.query(
`
SELECT * FROM shorturl
WHERE
final_key = ?
`,
[finalKey]
);
result = responseCheckFinalKey[0][0];
// if url
while (result !== undefined) {
const randomKeyBackup = nanoid(5);
finalKey = randomKeyBackup + lastNumber;
const responseCheckFinalKey = await connection.query(
`
SELECT * FROM shorturl
WHERE
final_key = ?
`,
[finalKey]
);
result = responseCheckFinalKey[0][0];
}
// store the finalKey and original URL to DB
const responseStoreURL = await connection.query(
`
INSERT INTO shorturl (final_key, original_url)
VALUES
(?, ?)
`,
[finalKey, originalURL]
);
await connection.commit();
// return short URL
// res.send(`http://${host_ip}/${finalKey}`);
res.send({
status: "success",
message: "建立成功",
url: `http://${host_ip}/${finalKey}`,
});
} catch (error) {
await connection.rollback();
res.status(500).send(error);
} finally {
connection.release();
}
// mongo
// try {
// let shortUrl = finalKey;
// // console.log('---hit shorturl---' , shortUrl)
// const result = await db
// .collection("urls")
// .insertOne({ shortUrl, originalUrl });
// // console.log('result id -->',result);
// res.send({
// status: "success",
// message: "建立成功",
// url: `http://${host_ip}/${shortUrl}`,
// });
// } catch (err) {
// console.log(err);
// res.status(500).send("發生錯誤");
// }
});
// GET API for getting original URL and redirect to it
app.get("/:finalKey", checkCache, async (req, res) => {
// const finalKey = req.params.finalKey;
if (checkRedisConnected) {
// mysql
try {
const finalKey = req.params.finalKey;
const finalKeyLastNumber = finalKey[finalKey.length - 1];
let originalURL = "";
// check the short URL being stored at which DB
if (finalKeyLastNumber === "1") {
originalURL = await getOriginalURL1(finalKey);
} else if (finalKeyLastNumber === "2") {
originalURL = await getOriginalURL2(finalKey);
} else if (finalKeyLastNumber === "3") {
originalURL = await getOriginalURL3(finalKey);
}
await redisClient.set(finalKey, originalURL);
// redirect to the original URL
console.log("Redirect to", originalURL);
res.redirect(originalURL);
} catch (error) {
res.status(500).send(error);
}
// mongodb
// try {
// const db = client.db("urls");
// const result = await db
// .collection("urls")
// .findOne({ shortUrl: finalKey });
// if (result) {
// const originalUrl = result.originalUrl;
// console.log("original URL: ", originalUrl);
// await redisClient.set(finalKey, originalUrl);
// return res.status(200).redirect(originalUrl);
// } else {
// return res.status(404).send("URL not found");
// }
// } catch (err) {
// console.log("err", err);
// return res.status(500).send("Error retrieving URL");
// }
} else {
// mysql
try {
const finalKey = req.params.finalKey;
const finalKeyLastNumber = finalKey[finalKey.length - 1];
let originalURL = "";
// check the short URL being stored at which DB
if (finalKeyLastNumber === "1") {
originalURL = await getOriginalURL1(finalKey);
} else if (finalKeyLastNumber === "2") {
originalURL = await getOriginalURL2(finalKey);
} else if (finalKeyLastNumber === "3") {
originalURL = await getOriginalURL3(finalKey);
}
// redirect to the original URL
console.log("Redirect to", originalURL);
res.redirect(originalURL);
} catch (error) {
res.status(500).send(error);
}
// mongodb
// try {
// const db = client.db("urls");
// const result = await db
// .collection("urls")
// .findOne({ shortUrl: finalKey });
// if (result) {
// const originalUrl = result.originalUrl;
// console.log("original URL: ", originalUrl);
// return res.status(200).redirect(originalUrl);
// } else {
// return res.status(404).send("URL not found");
// }
// } catch (err) {
// console.log("err", err);
// return res.status(500).send("Error retrieving URL");
// }
}
});
// listen on port:3000
app.listen("3000", () => {
console.log("Server is running and listening on port:3000...");
});