-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (59 loc) · 1.87 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
var express = require("express")
var app = express()
var bodyParser = require('body-parser')
var fs = require('fs')
var redis = require("redis").createClient()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: false
}))
app.set("view engine", "jade")
app.set("views", __dirname + "/views")
/*app.get("/api/subscribers", function(request, response)
{
redis.lrange("cookie:subscribers", 0, -1, function(err, data)
{
if (err) return response.sendStatus(500)
response.json(data)
})
})*/
app.post("/api/register", function (request, response) {
var handle = request.body.handle
if ((typeof handle !== "string") || (handle.length === 0))
return response.sendStatus(400)
handle = handle.substr(0, 100)
var group = request.body.group
if ((typeof group !== "string"))
return response.sendStatus(400)
group = group.substr(0, 100)
var visitor = handle;
if (group)
visitor += " / " + group;
redis.rpush("cookie2018:visitors", visitor, function (err) {
if (err) return response.sendStatus(500)
response.redirect("/visitors")
})
})
app.get("/", function (request, response) {
response.render("index")
})
app.get("/visitors", function (request, response) {
return redis.lrange("cookie2018:visitors", 0, -1, function (err, visitors) {
if (err) return response.sendStatus(500)
response.render("visitors", {
visitors: visitors,
})
})
})
app.get("/:page", function (request, response, next) {
fs.exists(__dirname + '/views/' + request.params.page + '.jade', function (exists) {
if (!exists) return next()
response.render(request.params.page)
})
})
app.use(express.static(__dirname + "/public"))
app.use(function (err, request, response, next) {
console.log(err)
response.sendStatus(500)
})
app.listen(8092)