Vadorequest
Vadorequest

Reputation: 17997

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.

I ue sails.js, I know I have to do that with the config/routes.js, I just don't know how.

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

Upvotes: 0

Views: 1956

Answers (2)

sgress454
sgress454

Reputation: 24948

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. All requests to /backoffice/user/:action will then be automatically routed to that controller.

Upvotes: 1

Chris McClellan
Chris McClellan

Reputation: 1105

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.

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)

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

and then in your 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.

Upvotes: 1

Related Questions