I want to namespace my api requests to /api/v1/ Maybe later some also to api/v2/. How can I do this efficiently in sails.js?
There is three ways of doing this.
1st: blueprints
http://sailsjs.org/#!/documentation/reference/sails.config/sails.config.blueprints.html how to create a global route prefix in sails?
prefix: '/api'
or restPrefix: '/api'
how to create a global route prefix in sails?
2nd: in each controller adding
_config: { prefix: '/api/v2' }
3rd: configure it in the routes
http://sailsjs.org/#!/documentation/concepts/Routes
'/api/v2/': 'FooController',
Whereas other frameworks allow you to nest a block or closure, you can't do so in Sails. My approach is to use a variable that holds the prefix and apply it (after evaluating the string) to each route object key as such:
const prefix = '/my/api/v2';
module.exports = {
[`GET ${prefix}/where/ever/you/want`]: { ... },
[`POST ${prefix}/some/where/nice`]: { ... },
}
The above uses string interpolation with ES6. If you do not have that, just use string concatenation: ['GET ' + prefix + '/where/ever']: { ... }
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With