Created
December 6, 2011 15:27
-
-
Save xsyn/1438583 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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