How to use a callback array as the handler for a Sails.js route?

坚强是说给别人听的谎言 提交于 2019-12-13 07:33:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!