-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
77 lines (70 loc) · 1.87 KB
/
main.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
//TASK 01//
const express = require("express");
const app = express();
const shortid = require("shortid");
const mysql = require("mysql");
const port = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
// Connect to DB
const con = mysql.createConnection({
host: "localhost",
database: "task01",
user: "root",
password: ""
});
con.connect((err) => {
if (err) {
throw err
return;
}
console.log("Connected to DB!");
});
// Routes
app.get("/", (req , res) => {
res.render('index.ejs', { shortUrl: null });
});
app.post("/shorturl", (req, res) => {
const {originalUrl} = req.body;
const sql = "SELECT * FROM url WHERE originalurl = ?";
con.query(sql, [originalUrl], (err, result) => {
if (err) {
throw err
return;
}
if (result.length > 0) {
res.render('index.ejs', { shortUrl: `${req.headers.host}/${result[0].shorturl}` });
} else {
const shortUrl = shortid.generate();
const sql2 = 'INSERT INTO url (originalurl, shorturl) VALUES (?, ?)';
con.query(sql2, [originalUrl, shortUrl], (err, result) => {
if (err) {
throw err
return;
}
res.status(201).render('index.ejs', { shortUrl: `${req.headers.host}/${shortUrl}` });
});
}
});
});
app.get("/:shorturl", (req, res) => {
const {shorturl} = req.params;
const sql3 = "SELECT originalurl FROM url WHERE shorturl = ?";
con.query(sql3, [shorturl], (err, result) => {
if (err) {
throw err
return;
}
if (result.length > 0) {
const originalUrl = result[0].originalurl;
res.redirect(originalUrl);
} else {
res.status(404).send("SHORT URL NOT FOUND!!!!");
}
});
});
// server port
app.listen(port, () => {
console.log("NOW ON PORT 3000");
});