Skip to content

Instantly share code, notes, and snippets.

@xsyn
Created December 6, 2011 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xsyn/1438583 to your computer and use it in GitHub Desktop.
Save xsyn/1438583 to your computer and use it in GitHub Desktop.
// 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