Question

As the title suggests, I would like to assign multiple action handlers within different controllers for the same route in Sails Js

What I have tried:

"post /rest/users": [{
    controller: "FirstController",
    action: "someAction"
},
{
    controller: "SecondController",
    action: "otherAction"
}]

But this gives me error 51 in Sails, and neither action is reached (when debugged). I searched for this solution but I couldn't find something.

I know that Sails uses Express's route middleware and I found that this can be done in Express with the following route:

app.post('/users', createUser, saveToDB, endTheResponse, sendEmail);

I know some possible workarounds (like requiring the controller within the first one) but that's not what I'm looking for, and I couldn't adapt the Express example route to Sails one.

Any suggestions are welcomed! Thanks.

Was it helpful?

Solution

Ok, after searching for over than 1 week, I found the solution here. So everyone who's dealing with this and have problems calling multiple controllers here's how this can be achieved. From the gist:

Routes can now specify a list of targets, which will be run in order (this allows for binding chains of middleware directly to routes). Both controllers (controller.action) and arbitrary middleare (middleware) functions from the new, optional middleware directory can be specified, in any order:

{
    'post /signup': ['user.unique', 'user.create', 'verifyemail.send'],
    '/auth/logout': ['authenticated', 'auth.logout']
}

You can also still use classic {controller: 'foo', action: 'bar' } notation. And conveniently, miscellaneous strings are treated as redirects:

{
    // Alias '/logout' to '/auth/logout'
    'get /logout': '/auth/logout',
    '/thegoogle': 'http://google.com'
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top