-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.js
48 lines (43 loc) · 1.09 KB
/
home.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
const homeRouter = require("express").Router();
const ejs = require("ejs");
const {shortenUrl, getUrl} = require("./myModule/urls");
module.exports = homeRouter;
homeRouter.get("/", (req, res, next) => {
let ctx = {};
res.render("index.ejs", ctx);
});
const paramValidate = (req, res, next) => {
let val = req.url.substr(5);
let shortened = null;
let formed = /^(https?:\/\/)?(www\.)?[^(www\.)]+\.[\s\S]+$/.test(val);
if(!formed){
if(req.query.allow !== "true"){
return res.send(JSON.stringify({
"error": "URL invalid"
}));
}else{
shortened = shortenUrl(req, val);
}
}else{
shortened = shortenUrl(req, val);
}
req.ourl = val;
req.surl = shortened;
next();
};
homeRouter.get(/new\/[\s\S]+/, paramValidate, (req, res, next) => {
let rt = {};
rt["original_url"] = req.ourl;
rt["short_url"] = req.surl;
res.send(JSON.stringify(rt));
});
homeRouter.get("/:id", (req, res, next) => {
let rt = {};
let surl = getUrl(req.params.id);
if(!surl){
rt["error"] = "No short url found for given input";
}else{
return res.redirect(301, surl);
}
res.send(JSON.stringify(rt));
});