-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (42 loc) · 1.24 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
var express = require("express");
var bodyParser = require("body-parser");
var sequelize = require("sequelize");
var Hashids = require("hashids");
hashids = new Hashids("Emma's salt");
var db = require('./models');
var app = express();
var ejsLayouts = require("express-ejs-layouts");
app.use(ejsLayouts);
app.set("view engine", "ejs");
app.use(express.static(__dirname + '/static'));
app.use(bodyParser.urlencoded({extended: false}));
app.set("view engine", "ejs");
app.get("/", function(req, res) {
res.render("index");
});
app.get("/error", function(req, res) {
res.render("error");
});
app.get("/links/", function(req, res) {
var urls = db.url.findAll({order: 'clicks DESC'}).then(function(urls) {
var myArray = urls;
res.render("links/index", {myArray});
});
});
app.get("/:hash", function(req, res) {
var hashid = req.params.hash;
db.url.find({where: {hash: hashid}}).then(function(url) {
url.clicks++;
console.log(url.clicks);
url.save().then(function(url) {
var finalUrl = url.url;
res.redirect(finalUrl);
});
});
});
app.get("/links/:idx", function(req, res) {
var id = req.params.idx;
res.render("links/show", {hash: id});
});
app.use("/links", require("./controllers/links"));
app.listen(process.env.PORT || 3000);