Skip to content

Instantly share code, notes, and snippets.

@bytespider
Created December 2, 2011 14:00
Show Gist options
  • Select an option

  • Save bytespider/1423328 to your computer and use it in GitHub Desktop.

Select an option

Save bytespider/1423328 to your computer and use it in GitHub Desktop.
var util = require("util");
exports.show = {
json: function (req, res) {
res.send(JSON.stringify(req.item));
},
default: function (req, res) {
this[req.format](req, res);
}
};
exports.load = function (id, callback) {
var redis = require("redis"),
client = redis.createClient();
client.smembers("items", function (err, ids) {
var items = [];
if (ids.length <= 0) {
callback(err, items);
client.quit();
}
var len = ids.length;
for (var i = 0; i < len; i++)
{
client.multi()
.get("items:" + ids[i] + ":title")
.get("items:" + ids[i] + ":description")
.smembers("items:" + ids[i] + ":keywords")
.smembers("items:" + ids[i] + ":categories")
.exec(function (err, replies) {
items.push({
id: ids[i],
title: replies[0],
description: replies[2],
keywords: replies[3],
categories: replies[4]
});
if (items.length == len)
{
callback(err, items);
client.quit();
}
});
}
});
};
var express = require('express'),
Resource = require('express-resource'),
app = express.createServer();
app.resource('item', require('./item'), { format: 'json' });
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment