-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (39 loc) · 1.28 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
// Given an object id, return all links
var redis = require('redis')
, client = redis.createClient()
, express = require('express')
, app = express()
, cors = require('cors')
app.use(cors())
app.get('/work/:id', function(req, res) {
var key = 'object:'+req.params.id+':links'
client.smembers(key, function(err, reply) {
res.render('object.jade', {id: req.params.id, links: reply.map(function(line) { return JSON.parse(line) })})
})
})
app.get('/json/:id', function(req, res) {
var key = 'object:'+req.params.id+':links'
client.smembers(key, function(err, reply) {
var links = reply.map(function(line) { return JSON.parse(line) })
res.json(200, links)
})
})
app.get('/', function(req, res) {
client.keys('*:links', function(err, objectLinks) {
var m = client.multi()
// Redis multi-query: batch all the queries up and
objectLinks.forEach(function(link) { m.smembers(link) })
// execute once
m.exec(function(err, replies) {
replies = replies.map(function(reply, index) {
return {
id: objectLinks[index].split(':')[1],
links: reply.map(function(line) { return JSON.parse(line) })
}
})
console.log(replies)
res.render('index.jade', {linkedObjects: replies})
})
})
})
app.listen(44444)