简体   繁体   中英

Falcor route specific middleware

Given the following Router class running on the server:

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'
      }
    ];
  }
}]);

And the following path query in the browser (I am using the falcor-http-datasource):

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

I get the correct data of:

{
  "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"}
    }
  }
}

My question is, on the server, is a way for me to get access to the actual results that falcor sends over the wire back to the browser in response to this get route request?

My use case is that I want to log out two pieces of data together:

  1. The pathSet that the route gets passed.
  2. The json result that falcor sends back over the wire.

I was thinking it might look something like this:

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'
      }
    ];
  }
}]);

Just to be clear. I know I can get the results in the client but I want to pipe them elsewhere on the server.

Easiest thing at the moment would be to just decorate the Router before sending it to the express middleware.

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));    
                    });
        }
    };
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM