-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
178 lines (141 loc) · 3.26 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
//jshint esversion:9
require('dotenv').config();
const express = require('express');
const bodyParser = require("body-parser");
const validUrl = require('valid-url');
const shortid = require('shortid');
const mongoose = require('mongoose');
const _ = require('lodash/string');
const app = express();
const urls = [{
shortUrl: String,
longUrl: String
}];
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('public'));
app.set('view engine', 'ejs');
//Connect to Database
const connectDB = async function(req, res) {
try {
await mongoose.connect(process.env.MONGO_CONNECT, {
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log('MongoDB Connected');
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
connectDB();
//Create UrlSchema
const urlSchema = new mongoose.Schema({
urlCode: String,
longUrl: String,
shortUrl: String,
date: {
type: String,
default: Date.now
}
});
const Url = mongoose.model('Url', urlSchema);
app.use(express.json({
extended: false
}));
//Routes
//GET Routes
app.get('/', function(req, res) {
res.render("home", {});
});
app.get('/:code', async function(req, res) {
try {
const url = await Url.findOne({
urlCode: req.params.code
});
if (url) {
return res.redirect(url.longUrl);
} else {
return res.status(404).json('No url found');
}
} catch (e) {
console.error(err);
res.status(500).json('Server error');
}
});
//POST Routes
app.post("/", function(req, res) {});
app.post('/shorten', async function(req, res) {
const {
longUrl
} = req.body;
const baseUrl = "http://bitzz.me";
//Is BaseUrl Valid?
if (!validUrl.isUri(baseUrl)) {
return res.status(401).json('Invalid base url');
}
// Create the Shortened Url Id Code
const urlCode = shortid.generate();
//If LongUrl is Valid create a shortned url,
// or return one that has been shortned in the past
if (validUrl.isUri(longUrl)) {
try {
let url = await Url.findOne({
longUrl
});
if (url) {
let loLongUrl = _.truncate(longUrl, {
'length': 30
});
if (urls.includes({
longUrl: loLongUrl,
shortUrl: url.shortUrl
})) {
res.render("success", {
urls: urls
});
}
// res.json(url);
urls.push({
longUrl: loLongUrl,
shortUrl: url.shortUrl
});
res.render("success", {
urls: urls
});
} else {
const shortUrl = baseUrl + '/' + urlCode;
url = new Url({
longUrl,
shortUrl,
urlCode,
date: new Date()
});
await url.save();
let loLongUrl = _.truncate(longUrl);
urls.push({
longUrl: loLongUrl,
shortUrl: url.shortUrl
});
res.render("success", {
urls: urls
});
}
} catch (err) {
console.error(err);
res.status(500).json('Server Error');
}
} else {
res.render("invalid", {
urls: urls
});
}
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 5000;
}
app.listen(port, function() {
console.log("Port running on server 5000");
});