-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (117 loc) · 4.32 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
156
157
158
159
160
161
162
163
164
165
166
var http = require('http');
var mongo = require('mongodb').MongoClient;
var url = process.env.MONGOLAB_URI;
var server = http.createServer();
server.on('request', function(request, response) {
var appAdresi = request.headers.host;
if (request.url.substring(0,5) == "/new/")
{
var verilen = request.url.substring(5, request.url.length);
console.log(verilen);
if (((verilen.substring(0,7) == "http://") || (verilen.substring(0,8) == "https://")) && (verilen.indexOf(".") > verilen.indexOf("//") + 2))
{
var tesadufi = makeid();
var ekle =
{
"original_url": verilen,
"short_url": tesadufi
};
var denemeObj =
{
"original_url": ekle.original_url,
"short_url": appAdresi + "/" + ekle.short_url
};
mongo.connect(url, function(err, db) {
if (err)
{
console.log("hata var aga");
process.exit(1);
}
var collection = db.collection('denemeConnection');
collection.insert(ekle, function(err, data) {
if (err)
{
console.log("hata var");
}
console.log(JSON.stringify(ekle));
});
db.close();
});
response.end(JSON.stringify(denemeObj));
}
else
{
response.end("Please enter a valid URL!");
}
}
else if (request.url.length == 1)
{
console.log("yonerge");
var denemeMesaji = "<h1> URL Shortener Microservice </h1>";
denemeMesaji += "<h3> To shorten: </h3>";
denemeMesaji += "<p>Pass a URL as a parameter after '/new/' and you will receive a shortened URL in the JSON response. <p>"
denemeMesaji += "<p> Sample use: " + appAdresi + "/new/https://www.google.com<br><br>";
denemeMesaji += "<h3> To fetch shortened URL: </h3>";
denemeMesaji += "<p>Simply visit the URL provided in the JSON response. <p>"
denemeMesaji += "<p> Sample use: " + appAdresi + "YUrX3";
response.end(denemeMesaji);
}
else if ((request.url.length == 6 && request.url[5] != '/') || (request.url.length == 7 && request.url[6] == '/'))
{
var bakilasi = request.url.substring(1, 6);
console.log("bakacagiz var mi " + bakilasi);
//console.log("bakacagiz var mi " + request.url.substring(1, 6));
mongo.connect(url, function(err, db) {
if (err)
{
console.log("hata var aga");
process.exit(1);
}
var collection = db.collection('denemeConnection');
var yanita = collection.find({
// "short_url": "QIQXH"
"short_url": bakilasi
}).toArray(function(err, documents) {
if (err)
{
console.log("hata var");
}
// console.log(documents);
if (documents.length == 0)
{
console.log("yokmus aga");
response.end("Please provide a valid URL to be shortened!");
}
else
{
var yonlendirmeye = documents[0].original_url;
//console.log(yonlendirmeye);
console.log(yonlendirmeye);
response.writeHead(302, {
'Location' : yonlendirmeye
});
response.end("redirect olucan mi acaba bebisim, database'imizde varsa sayet " + request.url.substring(1, 6));
}
});
db.close();
});
}
else
{
console.log("duzgun gir lam");
response.end("Please provide a valid URL to be shortened!");
}
});
server.listen(process.env.PORT, function() {
console.log('Node app is running on port', process.env.PORT);
});
function makeid()
{
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var text = "";
for ( var i=0; i < 5; i++)
{
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}