Falcor路由特定中间件

Falcor route specific middleware

本文关键字:中间件 路由 Falcor      更新时间:2023-09-26

给定服务器上运行的Router类:

var PetsRouterBase = Router.createClass([{
  route: 'petList[{integers:indices}].name',
  get: function(pathSet) {
    return [
      { 
        path: ['petList', 0, 'name'], 
        value: 'Pets I have now'
      },
      { 
        path: ['petList', 1, 'name'], 
        value: 'Pets I once had'
      },
      { 
        path: ['petList', 2, 'name'], 
        value: 'Pets my friends have'
      }
    ];
  }
}]);

以及浏览器中的以下路径查询(我使用的是falco -http-datasource):

model.get('petList[0..2].name');

我得到了正确的数据:

{
  "jsonGraph": {
    "petList": { 
      "0":{"name":"Shows of Artists I've been to before",
      "1":{"name":"Shows of artists my friends have been to before",
      "2":{"name":"Highly rated artists"}
    }
  }
}

我的问题是,在服务器上,是否有一种方法可以让我访问falcor在响应此get路由请求时通过有线发送回浏览器的实际结果?

我的用例是我想一起注销两段数据:

  1. 路由通过的路径集。
  2. falcor通过网络发回的json结果。

我在想它可能看起来像这样:

var PetsRouterBase = Router.createClass([{
  route: 'petList[{integers:indices}].name',
  done: function(pathSet, results) {
    // Log out the result of the lookup
    console.log(pathSet, results); 
  },
  get: function(pathSet) {
    return [
      { 
        path: ['petList', 0, 'name'], 
        value: 'Pets I have now'
      },
      { 
        path: ['petList', 1, 'name'], 
        value: 'Pets I once had'
      },
      { 
        path: ['petList', 2, 'name'], 
        value: 'Pets my friends have'
      }
    ];
  }
}]);

澄清一下。我知道我可以在客户端得到结果,但我想把它们管道到服务器的其他地方。

目前最简单的方法是在将路由器发送给快速中间件之前装饰它。

app.use('/model.json', FalcorServer.dataSourceRoute(function(req, res) {
    return {
        get: function(pathSets) {
            // print incoming paths to console
            console.log(JSON.stringify(pathSets, null, 4));
            return router.
                get(pathSets).
                // print the results to the console
                    doAction(function(output) {
                        console.log(JSON.stringify(output, null, 4));    
                    });
        }
    };
})