Meteor中的JSON端点

the*_*ire 6 meteor

有没有办法使用流星返回页面中的直接文本?假设有人请求domain.com/get/that-thing,我只想返回字符串"52",以便请求者知道事物有"52"的东西.根据我的理解,这在Meteor中是不可能的,因为总是包括标题等.

2个可行的黑客攻击:写入一个名为"that-thing"的文件,期待可以调用"那个东西".这在一般情况下不起作用.放置一个反向代理,将一些请求重定向到非流星后端.

有一个更好的方法吗?

nel*_*nic 13

我今天必须解决这个问题并使用Iron-Router服务器端路由:https://github.com/EventedMind/iron-router/blob/master/DOCS.md#server-side-routing

简单的例子:

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));
  }
});
});
Run Code Online (Sandbox Code Playgroud)

这将返回一个有效的JSON"页面",然后您可以根据需要使用它.

感谢@Akshat回答:没有布局模板或JSON视图的流星铁路由器


the*_*ire 0

路由器支持此功能;查看服务器端路由:https://github.com/tmeasday/meteor-router