1

Hi Im new to node & sails and the same time doing some standards in my codes. In sails.js I currently have this, for example below

api/
  controllers/
    TestController.js
  services/
    TestServices.js

Mostly I can access the view with using only this URL format:

http://localhost/test/
http://localhost/test/<action>

If I add some prefixes to my filename, it will become like this:

api/
  controllers/
    PrtestController.js
  services/
    PrtestServices.js

URL by right should be accessible via:

http://localhost/prtest/
http://localhost/prtest/<action>

Questions:

1) if I add prefixes to all my controller & services filenames, is it possible to access the url by:

http://localhost/test/
http://localhost/test/<action>

without adding prefix?

I was thinking of configuring config/routes.js in order to achieve this by editing something like this:

'/test': {
    controller: 'PrtestController',
    action: '<action>'
},
'/test/*': {
    controller: 'PrtestController'
} 

Honestly, I haven't tried it yet (it's just an idea before I make major changes in my codes else I might messed it up)

2) Is it possible to have this in sails.js

PrtestController.js  >   prTestController.js

Thanks in advance!

EDITED QUESTION

Btw I'm using default sails config for controllers having values of:

blueprints: {
actions: true,
rest: true,
shortcuts: true,
prefix: '' 
... 

} Example: (my routes.js will be looked like this)

'/test' : 'prTestController',
'/test/action2' : 'prTestController:action2',
'/test/action3' : 'prTestController:action3',
'/test/action4' : 'prTestController:action4',
'/test/action5' : 'prTestController:action5',
'/test/action6' : 'prTestController:action6',
'/test/action7' : 'prTestController:action7',
'/test/action8' : 'prTestController:action8',
'/test/action9' : 'prTestController:action9'

Is it possible for routes that if the url is having /test or /test/any_action will automatically used controller prTestController or prTestController:any_action, respectively?

For #2, yup that's what I mean.

Thanks much!

Community
  • 1
  • 1
Nina
  • 141
  • 2
  • 14

1 Answers1

1
  1. Yes, you have to edit the config/routes.js in order to do custom routing.
    If your controller is named PrtestController, then (if activated) the blueprints will automagically set up a route for you to host/prtest/. To override this, turn the blueprints off, and add some custom routes.
    Sails docs on routes

    If you are having difficulties understanding the magic in Sails, I suggest you to turn off all the blueprints, or at least play with the different settings. By doing this, you have to manually configure routes and actions. When you have an understanding of what the blueprint does and the reason for it, turn it back on if you want to use it.

    Here is a video tutorial explaining blueprints and routes in more detail.

  2. Not sure what you mean. Do you wonder if lowercase controllernames are allowed? If so, yes, that shouldn't be a problem.

aludvigsen
  • 5,893
  • 3
  • 26
  • 37
  • hi thanks for your reply. For #1 is there any solutions there without changing the routes? Coz let me say I have 10 controllers with 10 different actions, so in my routes.js I will have at least 100 entries for manually routing? Example: '/test' : 'prTestController', '/test/index2' : 'prTestController:index2', ... '/test/index9' : 'prTestController:index9' Is it possible for routes that if the url is having /test or /test/ will automatically used controller prTestController or prTestController:, respectively? – Nina Apr 22 '14 at 07:44
  • @Nina I'm not sure. I haven't tested that myself, so you will just have to try it out I guess :) I'm afraid you have to do them all though, when you turn off the blueprints. – aludvigsen Apr 22 '14 at 07:51
  • alright thanks much for your time :) it's quite painful too many of them lols 8) – Nina Apr 22 '14 at 07:57