【问题标题】:JSON endpoint in MeteorMeteor 中的 JSON 端点
【发布时间】:2015-11-22 15:55:00
【问题描述】:

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

2 个可行的技巧: 写入一个名为“that-thing”的文件,预期可能会调用“that-thing”。这在一般情况下不起作用。 放置一个反向代理,将一些请求重定向到非流星后端。

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我今天必须解决这个问题并使用 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));
      }
    });
    });
    

    这将返回一个有效的 JSON“页面”,然后您可以随意使用它。

    感谢@Akshat 的回答:Meteor Iron-Router Without Layout Template or JSON View

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • meteor-router 正式 停产。流星路由器 GitHub 存储库将用户指向 不支持 JSONgithub.com/EventedMind/iron-router。 :-( 编写自定义解决方案。如果您希望我分享,请告诉我)
      猜你喜欢
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多