-
Notifications
You must be signed in to change notification settings - Fork 47
/
redis.js
49 lines (38 loc) · 1.33 KB
/
redis.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
// based on https://github.com/visionmedia/express/blob/master/examples/session/redis.js
var http = require("http")
var Router = require("routes-router")
var Session = require("redsess")
var Redis = require("redis")
var sendHtml = require("send-data/html")
// create a single redis connection. This is shared among all
// req/res pairs in your server
var client = Redis.createClient()
var app = Router()
app.addRoute("/", function (req, res, opts, cb) {
var session = Session(req, res, { client: client })
// get a value of the session is a database read is async
session.get("views", function (err, views) {
if (err) {
return cb(err)
}
views = views || 0
views++
// setting a value in the session is a database write and
// is async
session.set("views", views, function (err) {
if (err) {
return cb(err)
}
var html = ""
if (views === 1) {
html += "<p>First time visiting?" +
" view this page in several browsers :)</p>"
}
sendHtml(req, res, html + "<p>viewed <strong>" +
views + "</strong> times.</p>")
})
})
})
var server = http.createServer(app)
server.listen(3000)
console.log("redis session server on port 3000")