In Sails, policies were designed to be applied to controllers and not individual routes. Thankfully, the framework is flexible enough that it's not too difficult to find workarounds for things like this.
There are a few ways to do this. Either way, you will need to catch the http request inside of the routes configuration file in config/routes.js
.
If you only need to apply one policy to the route/method, the easiest thing to do is put it directly inside of the routes config file. For example.
module.exports.routes = {
'/': {
view: 'homepage'
},
'get /user': function(req,res,next){
console.log('I made a get request!! ')
},
'post /user': function(req,res,next){
console.log('I made a post request!! ')
}
};
If you want to apply more than one policy to the route/method or you prefer keeping your policies in the policies folder, try something like below.
var policyOne = require('../api/policies/policyOne');
var policyTwo = require('../api/policies/policyTwo');
module.exports.routes = {
'/': {
view: 'homepage'
},
'get /user': [policyOne,policyTwo]
};