Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix all routes #2609

Closed
Kikobeats opened this issue Feb 2, 2015 · 5 comments
Closed

Prefix all routes #2609

Kikobeats opened this issue Feb 2, 2015 · 5 comments

Comments

@Kikobeats
Copy link

At this moment I want to prefix all routes of my api, so I have in blueprints this:

blueprints:
  actions: false
  shortcuts: false
  prefix: '/v1'

Works fine, but only prefix blueprints methods (obviously).

Exist a way to prefix all routes? I know that in express 4 is possible prefix easily with:

app.use('/v1', routerObject);

I know that is possible write a policie, but just I want to set up in the config...

Sorry if is possible to prefix now, but I don't see nothing in the documentation.

@sgress454
Copy link
Member

Hi @Kikobeats, it's not entirely clear what you're trying to do here--if you put actions back to true, then all of your routes mapped automatically to controller actions will have the prefix. Routes that you define in config/routes.js won't ever be prefixed, because you are after all explicitly stating what you want the path to be!

@Kikobeats
Copy link
Author

@sgress454 exactly I want to do is something to avoid explicity prefix the routes in config/routes.js and prefix all routes (like is possible in blueprints with prefix option, but out of blueprints as well).

why? because when you are creating different versions of the same API (/v1, /v2/, ...) can be useful for streamline the process just changing one value that define the global prefix instead of all individuals routes.

Do you think that can be useful?

@swelham
Copy link

swelham commented Feb 16, 2015

Would be nice to have something similar to rails namespaces for this

@sgress454
Copy link
Member

I actually worked on this on a flight a few weeks ago, then lost it and forgot to post. So, here it is:

You can do this with policies--just not with the config/policies.js file. Instead, you need to inject the policy directly into config/routes.js as a wildcard:

'/*': {policy: 'selectApiVersion', skipAssets: true},

Then in api/policies/selectApiVersion:

module.exports = function(req, res, next) {

  // Alter the URL based on the desired API version
  req.url = "/v" + (sails.config.apiVersion || 1) + req.url;
  return next();

};

At the time, there was an issue where skipAssets wouldn't work with the policy route syntax. That's been patched on master, so the above will work, but we'll need to push out a patch release of Sails to make it available on NPM.

@Kikobeats
Copy link
Author

Sounds good! thanks for the clarification :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants