-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortly.js
165 lines (134 loc) · 3.88 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
var express = require('express');
var util = require('./lib/utility');
var partials = require('express-partials');
var bodyParser = require('body-parser');
var session = require('express-session');
var bcrypt = require('bcrypt-nodejs');
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.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(partials());
// Parse JSON (uniform resource locators)
app.use(bodyParser.json());
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}));
// Parse forms (signup/login)
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.get('/', util.authUser,
function(req, res) {
res.render('index');
});
app.get('/create', util.authUser,
function(req, res) {
console.log('GET the create page');
res.render('index');
});
app.get('/links', util.authUser,
function(req, res) {
Links.reset().fetch().then(function(links) {
res.status(200).send(links.models);
});
});
app.get('/signup',
function(req, res) {
res.render('signup');
}
);
app.get('/login',
function (req, res) {
res.render('login');
}
);
app.post('/links',
function(req, res) {
var uri = req.body.url;
if (!util.isValidUrl(uri)) {
console.log('Not a valid url: ', uri);
return res.sendStatus(404);
}
new Link({ url: uri }).fetch().then(function(found) {
if (found) {
res.status(200).send(found.attributes);
} else {
util.getUrlTitle(uri, function(err, title) {
if (err) {
console.log('Error reading URL heading: ', err);
return res.sendStatus(404);
}
Links.create({
url: uri,
title: title,
baseUrl: req.headers.origin
})
.then(function(newLink) {
res.status(200).send(newLink);
});
});
}
});
});
app.post('/signup', util.createUser,
function (req, res) {
console.log('trying to send the index page');
res.redirect('/');
});
app.post('/login', function(req, res) {
// var username = req.body.username;
// var password = req.body.password;
new User({username: req.body.username}).fetch().then(function(found) {
if (found) {
bcrypt.compare(req.body.password, found.attributes.password, function(err, correct) {
if (err) {
console.log(err);
}
if (correct) {
req.session.regenerate(function() {
req.session.user = req.body.username;
res.redirect('/');
});
} else {
res.redirect('/login');
}
});
} else {
res.redirect('/signup');
}
});
});
app.get('/logout', function(req, res) {
req.session.destroy(function() {
res.redirect('/login');
});
});
/************************************************************/
// Write your authentication routes here
/************************************************************/
/************************************************************/
// 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({
linkId: link.get('id')
});
click.save().then(function() {
link.set('visits', link.get('visits') + 1);
link.save().then(function() {
return res.redirect(link.get('url'));
});
});
}
});
});
module.exports = app;