-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (50 loc) · 1.18 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
var http = require("http")
module.exports = {
shorten: function(url, cb) {
if (typeof cb === "function") {
http.get('http://tinyurl.com/api-create.php?url=' + encodeURIComponent(url), res => {
var data = '';
res.on('data', chunk => {
data += chunk;
})
res.on('end', () => {
cb(data);
})
}).on("error", err => {
cb(null, err)
})
} else {
return new Promise((resolve, reject) => {
http.get('http://tinyurl.com/api-create.php?url=' + encodeURIComponent(url), res => {
res.on('data', chunk => {
resolve(chunk.toString())
})
}).on("error", err => {
reject(err)
})
})
}
},
resolve: function(url, cb) {
if (typeof cb === "function")
http
.get(url, res => {
if (res.headers.location) cb(res.headers.location);
else cb(null, new Error("Tiny URL not found!"));
})
.on("error", err => {
cb(null, err);
});
else
return new Promise((resolve, reject) => {
http
.get(url, res => {
if (res.headers.location) resolve(res.headers.location);
else reject(new Error("Tiny URL not found!"));
})
.on("error", err => {
reject(err);
});
});
}
};