Skip to content

Instantly share code, notes, and snippets.

@xsyn

xsyn/gist:1438583

Created Dec 6, 2011
Embed
What would you like to do?
// View a dream
app.get('/dreams/:id?.:format?', function(req, res) {
dreamModel.findById(req.params.id, function(err, docs) {
// Try and get images returning from db
console.log("Returning docs object for attachments: " + eyes.inspect(docs))
console.log("Checking for Attachments!!!");
if (docs._attachments) {
var promise = new(events.EventEmitter), response = {};
var streamer = db.getAttachment(docs._id, docs.filename);
streamer.addListener('response', function (res) {
response.headers = res.headers;
response.headers.status = res.statusCode;
response.body = "";
});
streamer.addListener('data', function(chunk) {
return response.write(chunk, "binary");
});
streamer.addListener('end', function() {
promise.emit('success', response);
});
console.log('Promise: ' + promise);
}
switch(req.params.format) {
case 'json':
console.log('Request for dream: ' + req.params.id + ' information.');
console.log('Document response: ' + docs);
res.send(JSON.stringify(docs));
break;
default:
res.render('dreams/view.jade', {
locals: { title : 'Keep your dream up to date',
doc : docs }
});
};
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.