How do I setup an api to access my meteor collecti

2020-05-06 16:54发布

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?

标签: meteor
1条回答
家丑人穷心不美
2楼-- · 2020-05-06 17:31

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();

  },
});

Notice that this code is for Meteor 0.6.5.

查看更多
登录 后发表回答