0

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.

jpgc
  • 764
  • 5
  • 19

1 Answers1

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.

sgress454
  • 24,870
  • 4
  • 74
  • 92
  • thanks, it worked as I needed it to. It's a great way of taking complexity away from the controllers and re-utilize code; just a question: why are they called `policies` instead or `middleware` or something similar? – jpgc Mar 04 '14 at 00:29
  • Two reasons: first, because they were originally used mainly for access control, and second, to avoid confusion with Express middleware (which you can also add to Sails projects via config/express.js). – sgress454 Mar 04 '14 at 00:33