Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js regex routes

I am building a simple sails.js project and implementing the front end with backbone.

Ideally I want a single route to the one index page in which my backbone app is served.

'/*': {
    view: 'home/index'
}

This is great, so any URL now goes to the homepage. Except now, all the routes to any assets (.js, .css, .html, .jpg) do not work anymore.

I can see this comment in the config.routes.js:

// NOTE:
// You'll still want to allow requests through to the static assets,
// so we need to set up this route to ignore URLs that have a trailing ".":
// (e.g. your javascript, CSS, and image files)
'get /*(^.*)': 'UserController.profile'

But it doesn't make any sense to me. How do I ignore routes with a file extensions.

I have also prefixed all my CRUD url's with 'api', localhost:1337/api/controller/ so a regex route to exclude forwarding those would also be required. I cannot find any information on how to do this anywhere.

I must be missing something fundamental here.

Thanks!

like image 200
Matt Derrick Avatar asked Nov 07 '13 18:11

Matt Derrick


2 Answers

You can use the skipAssets and skipRegex flags. This will not step on your static assets and will not block your api urls starting with /api. I use this with sails.js and angular in html5Mode.

  "get *":{
    controller:"PagesController",
    action:"index",
    skipAssets: true,
    skipRegex: /^\/api\/.*$/
  }
like image 92
Lenny Avatar answered Sep 21 '22 22:09

Lenny


I think this is a bug but I found a workaround solution.

'get /[^.?]+?': 'UserController.profile'

or you can use other one. only /user and /profile will exec UserController.profile. static directory still work great.

'get /:action( user | profile)': 'UserController.profile'

like image 38
Isken Huang Avatar answered Sep 18 '22 22:09

Isken Huang