简体   繁体   English

通过路由sails.js重定向请求

[英]Redirect requests via routes sails.js

I would like to have a sub part in my application: For instance, all requests send to www.example.com/backoffice/user should be redirected in my BackofficeUserController.js . 我想在我的应用程序中有一个子部分:例如,发送到www.example.com/backoffice/user所有请求都应该在我的BackofficeUserController.js重定向。

I ue sails.js, I know I have to do that with the config/routes.js, I just don't know how. 我是sails.js,我知道我必须使用config / routes.js这样做,我只是不知道如何。

I tried this: 我试过这个:

'/backoffice/:controller/:action?': {
    controller    : 'backoffice' + ':controller',
    action        : ':action'
}

But it doesn't works. 但它不起作用。 Any idea? 任何想法? The doc doesn't explains too much about dynamic routes. 该文档没有解释太多关于动态路线的内容。 http://sailsjs.org/#!documentation/routes http://sailsjs.org/#!documentation/routes

All captured params get passed to the controller's action method on your request object. 所有捕获的参数都会传递给请求对象上的控制器操作方法。 Maybe you should be more explicit when defining your routes or use your UserController as a proxy. 也许你在定义路线或使用UserController作为代理时应该更明确。

You could have backoffice users? 你可以有后台用户吗?

'/user/backoffice': 'UserController.backoffice'

or having a backoffice controller handle user requests 或者让后台控制器处理用户请求

'/backoffice/user/:id': 'BackofficeController.user'

or (i'm not sure if controllers are global but you could require the controller from another controller and use its methods inside UserController) 或者(我不确定控制器是否是全局控制器,但您可能需要来自另一个控制器的控制器并在UserController中使用其方法)

module.exports = {
  '/backoffice/user/:id': 'UserController.backoffice'
};

and then in your UserController 然后在你的UserController中

var BackofficeController = require('./BackofficeController');

module.exports = {
  user: function(req, res) {
   // Do something using BackOffice methods conditionally?
  }
};

Many ways to achieve the same result. 许多方法可以达到同样的效果。 Not sure what the best approach is since I haven't run into this personally. 不知道最好的方法是什么,因为我没有遇到过这个问题。 But I would suggest sticking with Sailsjs conventions. 但我建议坚持使用Sailsjs惯例。

This is actually a decent use-case for nested controllers. 对于嵌套控制器来说,这实际上是一个不错的用例。 If you do sails generate controller backoffice/user , you'll end up with a controllers/backoffice/userController.js file corresponding to a controller class called Backoffice/UserController . 如果你做sails generate controller backoffice/user ,你将得到一个controllers/backoffice/userController.js文件对应一个名为Backoffice/UserController的控制器类。 All requests to /backoffice/user/:action will then be automatically routed to that controller. 然后,对/backoffice/user/:action所有请求将自动路由到该控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM