Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Sails Controller to use only 'Post' method

Tags:

sails.js

I saw that all controllers methods are free for GET and POST. How to ensure to permits only POST for some methods?

like image 231
Alexnaldo Santos Avatar asked Aug 09 '14 11:08

Alexnaldo Santos


1 Answers

If you are using action blueprints to automatically route URLs to custom controller action, then those actions will respond to GET, PUT, POST, DELETE and PATCH methods by default. If you'd rather control which methods are allowed, you have a few choices:

  1. Disable certain methods using custom routes in your config/routes.js file. For example, if you have a foo action in UserController.js that you don't want to allow GET requests for, you can add the following custom route:

    "GET /user/foo": {response: 'forbidden'}
    

    to automatically route it to the "forbidden" response (same as doing res.forbidden() in a controller)

  2. Test req.method within the action itself, and return early for methods you don't want to process:

    if (req.method.toUpperCase() == 'GET') {return res.forbidden();}
    
  3. Disable action routes by setting actions to false in your config/blueprints.js file. You'll then have to set up all your routes manually in your config/routes.js file.

like image 115
sgress454 Avatar answered Jan 02 '23 19:01

sgress454