问题
When using Express, it is possible to attach a callback array to a route like this:
app.get('/path', thisIsAnArrayOfFunctions);
And then, when making a request to http://route_to_server/path
each function inside thisIsAnArrayOfFunctions
is called.
Exactly how does that routing behaviour works in express? is it just an iteration through thisIsAnArrayOfFunctions
, passing the arguments req
, res
and next
?
Is it possible to achieve a simple implementation in Sails for this?
I know it works if I attach the routing as an express middleware, but I want to know if there's a solution using the Sails' (version 0.9.8) controller structure.
Thanks in advance.
回答1:
The Sails-y way of chaining functions to a route is by using policies. The idea is that your controller code should be the last stop in handling your route. Anything that might modify the response (like a login check, or something that could change the params) should be implemented as a policy, which is middleware that can call next
or send a response directly. Policies are mapped to controller actions, and multiple policies can be applied to a single action (or to all actions in a controller).
Docs for policies are here.
来源:https://stackoverflow.com/questions/22160043/how-to-use-a-callback-array-as-the-handler-for-a-sails-js-route