How to restrict http request type in sails.js?

635 Views Asked by At

In sailsjs blueprint api,By default, action route responds to all four types of requests - GET, POST, PUT, DELETE. I would like to deny only certain types of request to a route, while allowing the others to go through. For example, I may wish to accept only POST requests to the bar action at /foo/bar, while denying GET, PUT and DELETE requests to that route.

Although Policies can be used to restrict controller actions.

FooController: { bar: false, },

But,Here it denies all 4 types of requests or allow all 4 types of requests based on the same condition. What I want instead is to always deny 3 out of the 4 types of request, while always allowing the 4th type to go through.

Can this be done without resorting to disabling Blueprint and manually inputting the routes into /config/routes.js? If so how?

1

There are 1 best solutions below

0
munkee On

This is one for your routes file (/config/routes.js), and not your policy file as you have identified.

'POST /page/link': YourControllerName.ActionName,
'GET /': YourHomePageController.ActionName,
'PUT /login': UserController.login

The first 2 are examples showing the syntax. The final example shows how this would look in a typical setup controlling a login form submit. If you wish to allow multiples through e.g. POST and PUT then just add another line in with the same controller.action but change the method.

For further examples and also other syntax that can be used take a look here:

http://sailsjs.org/documentation/concepts/routes

Once you have the routes you want, you will then need to turn off the blueprint methods, which if you leave on will over-ride these routes and still allow access through.

Go to your config/blueprint.js file and change the following items:

actions: false
rest: false
shortcuts: false

Once again, further information on what these do and how they can be used can be found here:

http://sailsjs.org/documentation/reference/configuration/sails-config-blueprints