为相同的路由发送多个处理程序 [英] Sails multiple handlers for the same route

查看:98
本文介绍了为相同的路由发送多个处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,我想在Sails Js中为同一条路线分配不同控制器中的多个操作处理程序

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

我已经尝试过:

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

但是这样会给我Sails中的错误51,并且没有达到动作(调试时)。我搜索了这个解决方案,但是找不到任何东西。

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.

我知道Sails使用Express的路由中间件,我发现这可以在Express中完成,具有以下路由:

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);

我知道一些可能的解决方法(比如要求第一个控制器),但这不是我' m寻找,我无法适应Express的路线到Sails一个。

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.

欢迎任何建议!谢谢。

推荐答案

好的,搜索超过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:


路线现在可以指定一个目标列表,它将以
(这个允许中间件的链接直接到路由)。
新的可选中间件目录中的两个控制器(controller.action)和任意中间件
(中间件)函数可以以任何顺序指定



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




您还可以使用经典的 {controller:'foo',action:'bar'}
表示法。并且方便地,杂项字符串被视为
重定向:



{
    // Alias '/logout' to '/auth/logout'
    'get /logout': '/auth/logout',
    '/thegoogle': 'http://google.com'
}

这篇关于为相同的路由发送多个处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆