Sails multiple handlers for the same route

那年仲夏 提交于 2019-12-02 02:59:44

问题


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.


回答1:


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'
}


来源:https://stackoverflow.com/questions/20025783/sails-multiple-handlers-for-the-same-route

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