How do I setup an api to access my meteor collection?

浪子不回头ぞ 提交于 2019-12-20 07:18:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/18499625/how-do-i-setup-an-api-to-access-my-meteor-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!