forked from WDI-SEA/link-shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (48 loc) · 1.29 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
var express = require('express');
var bodyParser = require('body-parser');
var ejsLayouts = require('express-ejs-layouts');
var db = require('./models');
var app = express();
var count;
app.set('view engine', 'ejs');
app.use(ejsLayouts);
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(__dirname + '/public/'));
var Hashids = require('Hashids');
var hashids = new Hashids('my shortie');
app.get("/", function(req, res){
res.render("index");
});
app.get("/links/:id", function(req, res) {
var link = req.params.id;
db.links.findById(link).then(function(links){
res.render("links.ejs", {
links: links
});
});
});
app.get("/:hash", function(req, res) {
var hash = req.params.hash;
db.links.find({where:{linkHash:hash}})
.then(function(row){
row.count++;
console.log("*******count: ", row.count);
row.updateAttributes({count: row.count});
res.redirect(row.linkUrl);
});
});
app.post("/", function(req, res){
db.links.findOrCreate({
where: {linkUrl: req.body.linkUrl}
})
.spread(function(links) {
var Hashids = hashids.encode(links.id);
links.updateAttributes({linkHash: Hashids, count: 0});
res.redirect('/links/'+links.id);
// res.send(links);
})
});
app.get("/popular", function(req, res) {
res.render("popular");
});
app.listen(3000);