-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (127 loc) · 4.6 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
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var config = require('./config');
var base58 = require('./base58.js');
var config = require('./config');
//var url = require('./models/url');
var mysql = require('mysql');
var async = require('async');
//use middleware body-parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// specify file path for our express server
app.use(express.static(path.join(__dirname,'controler')));
var connection = mysql.createConnection({
host : 'localhost',
user : '',
password : ''
});
//create table
connection.query('CREATE DATABASE IF NOT EXISTS url_shortener', function(err){
if (err) throw err;
connection.query('USE url_shortener', function(err){
if (err) throw err;
connection.query('create table if not exists urlmap( \
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, \
longurl varchar(255), \
timestamp DATETIME)',function (err) {
if (err) throw err;
});
});
});
// home page
app.get('/', function(req, res) {
//res.send("Welcome to URL Shortner Service!");
res.sendFile(path.join(__dirname,"/views/index.html"));
});
// route to create and return the shortened url given a long url
app.post('/api/shorten', function(req,res) {
var longUrl = req.body.url;
var shortUrl = '';
var result = "";
var urlId;
//check if url exists in db and if not create one
async.series(
[
function(callback) {
console.log('1st Function\n' );
connection.query('SELECT id FROM urlmap WHERE longurl = ?' , [longUrl], function(err,rows) {
if(err) throw err;
console.log("rows:: " , rows, "length: ", rows.length);
if (rows.length > 0) {
result = JSON.parse(JSON.stringify(rows));
urlId = result[0].id;
console.log("results: " , result, "\n id: ", urlId);
callback();
} else {
callback();
}
});
},
function (callback) {
console.log('2nd Function\n' );
if(result.length > 0) {
console.log("url already exists in DB");
callback();
} else {
console.log('Starting 2nd query\n' );
var timeStamp = new Date();
var data = {longurl: longUrl, timestamp: timeStamp};
connection.query("INSERT INTO urlmap SET ?" , data, function(err,rows,fields) {
if(err) throw err;
result = JSON.parse(JSON.stringify(rows));
urlId = rows.insertId;
console.log(`rows: ${rows} --\n result: ${result} -- urlId: ${urlId} \n fields: ${fields}`);
callback();
});
}
}
], function(err,results) {
console.log(`all functions complete -- results: ${results}`);
//create short url
shortUrl = config.webhost + base58.encode(urlId);
console.log("shortURL: ", shortUrl);
res.send({'shortUrl': shortUrl});
//connection.end();
});
});
// route to get short url and redirect user to the oriinal url
app.get('/:short_url', function(req,res){
var encoded_id = req.params.short_url;
var id = base58.decode(encoded_id);
console.log(`encoded_id: ${encoded_id} -- id: ${id} `);
var longUrl = '';
//look for id in DB
async.series(
[
function(callback) {
console.log('1st Function\n' );
connection.query('SELECT longurl FROM urlmap WHERE id = ?' , [id], function(err,rows) {
if(err) throw err;
if (rows.length > 0) {
result = JSON.parse(JSON.stringify(rows));
longUrl = result[0].longurl;
console.log("results: " , result, "\n longURL: ", longurl);
callback(null,longUrl);
} else {
callback(null,longUrl);
}
});
}
], function(err,results) {
console.log(`All redirect functions -- results: ${results}`);
console.log(`longUrl: ${results}`);
if (results) {
res.redirect(results)
} else {
res.status(400).send("Reqesut page not found");
}
//connection.end();
});
});
var port = process.env.PORT || 3000;
var server = app.listen(port, function() {
console.log(`Listening on port ${port}`);
});