【问题标题】:Use an Image Collection in Meteor在 Meteor 中使用图像集合
【发布时间】:2014-06-02 16:16:57
【问题描述】:
我正在构建一个 Meteor 应用程序,它通过带有 https://github.com/crazytoad/meteor-collectionapi 的 HTTP 请求与桌面客户端通信
桌面客户端以不规则的时间间隔生成图像,我希望 Meteor 站点仅显示最近生成的图像(最好是实时的)。我最初的想法是对带有 base64 图像数据的单例集合使用 PUT 请求,但我不知道如何在 Web 浏览器中将该数据转换为图像。注意:图像都非常小(远小于 1 MB),因此不需要使用 gridFS。
我意识到这个想法可能是完全错误的,所以如果我完全走错了路,请提出更好的行动方案。
【问题讨论】:
标签:
javascript
mongodb
meteor
meteorite
meteor-blaze
【解决方案1】:
您需要编写一个中间件来为您的图像提供正确的 MIME 类型。示例:
WebApp.connectHandlers.stack.splice (0, 0, {
route: '/imageserver',
handle: function(req, res, next) {
// Assuming the path is /imageserver/:id, here you get the :id
var iid = req.url.split('/')[1];
var item = Images.findOne(iid);
if(!item) {
// Image not found
res.writeHead(404);
res.end('File not found');
return;
}
// Image found
res.writeHead(200, {
'Content-Type': item.type,
});
res.write(new Buffer(item.data, 'base64'));
res.end();
},
});