没有布局模板或JSON视图的流星铁路由器

nel*_*nic 3 json meteor iron-router

使用流星铁路由器我怎样才能呈现数据作为JSON或简单地显示它的"原始"(没有布局模板)

基本上我希望能够做到这样的事情:

this.route('rawdata', {
  path: '/raw/:collection',
  layoutTemplate: 'nolayout',
  template: 'raw'
})
Run Code Online (Sandbox Code Playgroud)

where / raw/posts将显示Posts(集合)的原始json.

谢谢!

备注:

我知道Meteor中JSON端点 但是流星路由器已经停止,而Iron-Router似乎没有JSON端点功能.

我也看了一下https://atmospherejs.com/package/collection-api,但它不适合我的需要,因为我需要能够选择集合/记录的字段子集.

Aks*_*hat 10

制作原始布局模板

<template name="direct_layout">
    {{> yield}}
</template>
Run Code Online (Sandbox Code Playgroud)

然后使用它作为layoutTemplate直接使用您的模板.

this.route('rawdata', {
    path: '/raw/:collection',
    layoutTemplate: 'direct_layout',
    template: 'raw'
});
Run Code Online (Sandbox Code Playgroud)

我不确定您是否将其用作实际代码的占位符.如果您打算使用JSON或实际原始文本呈现数据.您可能需要考虑使用服务器端路由.你应该做这样的事情:

请注意,这是服务器端代码,与上面在客户端运行的代码不同:

this.route('serverRoute', {
  where: 'server',
  path: '/your-route',
  action: function() {
    var data = {this_is:'a json object'}


    this.response.writeHead(200, {'Content-Type': 'application/json'});
    this.response.end(JSON.stringify(data));
  }
});
Run Code Online (Sandbox Code Playgroud)

看看Iron-Router上的服务器端渲染:https://github.com/EventedMind/iron-router/blob/master/DOCS.md#server-side-routing