JSON endpoint in Meteor

我怕爱的太早我们不能终老 提交于 2020-01-01 05:23:10

问题


Is there a way to return straight text in a page using meteor? Say someone requested domain.com/get/that-thing, and I just wanted to return the string "52", so that the requester knows that-thing has "52" of something. To my understanding, this is not possible in Meteor because the headers and such are always included.

2 hacks that would work: Write to a file named "that-thing" in anticipation that "that-thing" might be called. This doesn't work in the general case. Put a reverse proxy that redirects some of the requests to a non-meteor backend.

Is there a better way to do this?


回答1:


I had to solve this today and using Iron-Router server-side-routing: https://github.com/EventedMind/iron-router/blob/master/DOCS.md#server-side-routing

Simple example:

Router.map(function () {
  this.route('api', {
    path: '/api',
    where: 'server',
    action: function () {
      var json = Collection.find().fetch(); // what ever data you want to return
      this.response.setHeader('Content-Type', 'application/json');
      this.response.end(JSON.stringify(json));
  }
});
});

This will return a valid JSON "page" which you can then use how ever you want.

Thanks to @Akshat for answering: Meteor Iron-Router Without Layout Template or JSON View




回答2:


Router supports this; check out the server side routing: https://github.com/tmeasday/meteor-router



来源:https://stackoverflow.com/questions/15601218/json-endpoint-in-meteor

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