-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortly.js
200 lines (167 loc) · 4.71 KB
/
shortly.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var express = require('express');
var util = require('./lib/utility');
var partials = require('express-partials');
var bodyParser = require('body-parser');
// Added as part of our solution
var knex = require('knex');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var db = require('./app/config');
var Users = require('./app/collections/users');
var User = require('./app/models/user');
var Links = require('./app/collections/links');
var Link = require('./app/models/link');
var Click = require('./app/models/click');
var app = express();
app.use(session({secret: 'whatever',
proxy: true,
resave: true,
saveUninitialized: true}));
var sess;
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(partials());
// Parse JSON (uniform resource locators)
app.use(bodyParser.json());
// Parse forms (signup/login)
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.get('/',
function(req, res) {
// if session exist then render home page
if (req.session.username) {
res.render('index');
res.send(200);
}
else {
// else, go to login
res.redirect('login');
res.end();
}
});
app.get('/login', function(req, res){
res.render('login');
res.end();
});
app.get('/create',
function(req, res) {
res.redirect('login');
res.end();
});
app.get('/links',
function(req, res) {
// if session exist then render home page
if (req.session.username) {
res.render('index');
}
else {
// else, go to login
res.redirect('login');
res.end();
}
});
app.post('/signup', function(req, res) {
// ****** Future: add verification of non-blank username and password
var un = req.body.username;
var pw = req.body.password;
var user = new User({'password': pw, 'username': un})
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser('whatever'));
// app.use(expressSession({secret : 'whatever',
// resave: true,
// saveUninitialized: true}));
user.save().then(function(newUser) {
Users.add(newUser);
sess = req.session;
sess.username = un;
sess.password = pw;
});
res.redirect('/');
res.end();
// store the username and the password in a session
});
app.post('/links',
function(req, res) {
var uri = req.body.url;
if (!util.isValidUrl(uri)) {
console.log('Not a valid url: ', uri);
return res.send(404);
}
new Link({ url: uri }).fetch().then(function(found) {
if (found) {
res.send(200, found.attributes);
} else {
util.getUrlTitle(uri, function(err, title) {
if (err) {
console.log('Error reading URL heading: ', err);
return res.send(404);
}
var link = new Link({
url: uri,
title: title,
base_url: req.headers.origin
});
link.save().then(function(newLink) {
Links.add(newLink);
res.send(200, newLink);
});
});
}
});
});
/************************************************************/
// Write your dedicated authentication routes here
// e.g. login, logout, etc.
/************************************************************/
app.post('/login', function(req, res){
// check if req.username is in db
// check if req.pass is in db
// if so, then redirect to '/'
// else, redirect to '/login'
var un = req.body.username;
var pw = req.body.password;
new User({username: un, password: pw}).fetch().then(function(found){
if(found){
// then start the session
sess = req.session;
sess.username = un;
sess.password = pw; // do we need this?
res.redirect('/');
}
else{
res.redirect('/login');
}
});
});
app.get('/signup', function(req, res){
});
/************************************************************/
// Handle the wildcard route last - if all other routes fail
// assume the route is a short code and try and handle it here.
// If the short-code doesn't exist, send the user to '/'
/************************************************************/
app.get('/*', function(req, res) {
new Link({ code: req.params[0] }).fetch().then(function(link) {
if (!link) {
res.redirect('/');
} else {
var click = new Click({
link_id: link.get('id')
});
click.save().then(function() {
db.knex('urls')
.where('code', '=', link.get('code'))
.update({
visits: link.get('visits') + 1,
}).then(function() {
return res.redirect(link.get('url'));
});
});
}
});
});
console.log('Shortly is listening on 4568');
app.listen(4568);