如何设置 api 来访问我的流星收藏?
问题描述:
使用另一个网站,我想知道有多少文档与我的流星应用程序中的查询匹配(通过某种 API 调用?).实现这一目标的最佳方法是什么?
Using another website, i would like to know how many documents match a query in my meteor application (through an api call of some sort?). What is the best way to achieve this?
答
最简单的方法是将 item 添加到 WebApp 堆栈中,例如:
The simplest way to do so is to add item to WebApp stack, for example:
WebApp.connectHandlers.stack.splice (0, 0, {
route: '/api/path',
handle: function(req, res, next) {
var count = Documents.find({}).count();
res.writeHead(200, {
'Content-Type': 'application/json',
});
res.write(JSON.stringify({count: count}));
res.end();
},
});
请注意,此代码适用于 Meteor 0.6.5.
Notice that this code is for Meteor 0.6.5.