0

Can we use regexp in routes map? I am creating a simple file browser. I have this route set in my /config/routes.js:

map.all('/assets/:folder.:format?', 'assets#index');

But the folder parameter could be (for example) Images/Logos, so I am interested if I can use regular expression for one.

In my ROR project I was able to solve the similar issue with the help of router' :contrains parameter:

match 'applications/:store/:platform/:identifier/:filename' => 'assets#direct_download',
    :constraints => {
      :store => /[\w.-]+/,
      :platform => /[\w.-]+/,
      :identifier => /[\w.-]+/,
      :filename => /.+/
    }

I was not able to find any examples. So I will be thankfull if somebody could clarify this for me.

shybovycha
  • 11,556
  • 6
  • 52
  • 82
jaromudr
  • 151
  • 8

1 Answers1

0

You could use this syntax:

map.all(new RegExp('^\/assets\/(.*)$'), 'assets#index');

And then capture the parameters with its index (starting from 0 - the whole string is ignored, unlike the preg_match or similar functions):

// app/controllers/assets_controller.js
console.log(params[0]);
shybovycha
  • 11,556
  • 6
  • 52
  • 82
  • Ok, do you know, may be there is posibility to have named parameters somehow? – jaromudr Jan 23 '13 at 15:45
  • @jaromudr you could modify the `railway-routes` nodejs module by your own to fix that. I see no sanity in the code that handles regexpy-routes now: https://github.com/1602/railway-routes/blob/master/lib/railway_routes.js#L53 – shybovycha Jan 23 '13 at 16:02