gpt4 book ai didi

node.js - 为同一路线 sails 多个处理程序

转载 作者:搜寻专家 更新时间:2023-11-01 00:11:13 24 4
gpt4 key购买 nike

正如标题所暗示的,我想在不同的 Controller 中为 Sails Js 中的同一路由分配多个 Action 处理程序

我尝试过的:

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

但这在 Sails 中给出了错误 51,并且没有达到任何操作(在调试时)。我搜索了此解决方案,但找不到任何内容。

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

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

我知道一些可能的解决方法(比如在第一个方法中需要 Controller )但这不是我想要的,而且我无法将 Express 示例路由调整为 Sails 路由。

欢迎任何建议!谢谢。

最佳答案

好的,在搜索了 1 个多星期后,我找到了解决方案 here .因此,处理此问题并在调用多个 Controller 时遇到问题的每个人都可以通过这里实现。从要点:

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

关于node.js - 为同一路线 sails 多个处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20025783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com