【发布时间】:2019-10-14 03:30:01
【问题描述】:
我有 express-graphql 构建的 graphql 服务器,并且我使用 mongoDb 将图像存储在普通数据库集合中,因为它们
我有 react 和 android 应用。将这些图片提供给客户的最佳方式是什么。
在我的架构中,我有类似的东西。
const productSchema = new Schema({
views: {type: Number, default: 0},
//Storing only thumbnail in this document.
thumbnail: {data: Buffer, contentType: String},
}); 在我的架构中,我有类似
const productType = new GraphQLObjectType({
name: 'Product',
fields: () => ({
//.........
thumbnail: {type: 'WHAT TO DO HERE'},
views: {type: new GraphQLNonNull(GraphQLInt)}
//.........
}),
});
编辑: 我创建了一个这样的路由器。
router.get('/t/:productId', function (req, res) {
const productId = req.params.productId;
Product.findById(productId, {thumbnail: true}).then(thumbnail => {
res.contentType(thumbnail.contentType);
res.end(thumbnail.data, "binary");
}).catch(e => {
console.error(e);
res.sendStatus(404);
});
});
【问题讨论】: