Question

So, I have a User model and a UserController.js file but with no functions in it. So the default REST endpoints are working for User model..

Now I am able to put a policy on the REST POST request on /user by putting the undergiven in my /config/policies.js file:

UserController: { "create" : 'authenticated', }

where 'authenticated' is the policy that I defined. So it seems that "create" is mapped to default REST "post /user" ...

Now my question is how do I put a policy for "get /user/:id" ... ? Is there a verb (like create for POST on /user) for /user/:id?

I hope I am clear enough..

Was it helpful?

Solution 2

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]
};

OTHER TIPS

According to this http://beta.sailsjs.org/#!documentation/reference/Blueprints/Blueprints.html these are the default actions available in blueprints:

-find
-findOne
-create
-update
-destroy
-populate
-add
-remove

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top