How to use sails.js models to search using a labelless REST project?

I have disabled shortcuts for shortcuts (/ model / findAll wont work) and now I want to search for a model using REST, but I have not found anything, is there any documentation or do I need to write my own views for what?

thanks

+7
source share
3 answers

In Sails.js , CRUD and REST design routes are included for development, but for production, I suggest you have your own actions that can find / create / update / delete models in this controller

Sails automatically map the actions in your controllers to the corresponding routes, without having to manually configure them to disable this: set the action flag to false in the file config / controllers.js / p>

This is best explained with an example, so here is a basic model with multiple RESTful routes

models /user.js

Our model has two attributes and does not provide uniqueness.

module.exports = { attributes: { name: 'string', email: 'string' } }; 

Controllers /UserController.js

Our controller has two actions: findAll returns all users, and findByName returns users with the given name

 module.exports = { findAll: function (req, res) { User.find().done(function (err, users) { if (err) { res.send(400); } else { res.send(users); } }); }, findByName: function(req, res) { var name = req.param('name'); User.findByName(name).done(function (err, users) { if (err) { res.send(400); } else { res.send(users); } }); } }; 

configurations /routes.js

The configuration of our routes has two routes that respond only to GET requests, / user / all will trigger the findAll action, and name / username / name /: the findByName action will be launched with the name specified as a parameter in the URL

 module.exports.routes = { 'GET /user/all': 'UserController.findAll', 'GET /user/name/:name': 'UserController.findByName' }; 

For more information, be sure to check the controller documentation in Sails.js

+12
source share

Just updating the previous post, since I could not find anyone who mentioned this, and I do not have enough reputation for comments, the connections.js file was deleted in later versions of Sails.js. These configuration options are now in the blueprints.js file.

+4
source share

Update scott answer using Promise, ES6 and custom answer

  findAll: function (req, res) { User.find().then(users => { res.send(users); }).catch(() => { res.badRequest('Error in GET'); }); }, findByName: function(req, res) { var name = req.param('name'); User.findByName(name).then(users => { res.send(users); }).catch(() => { res.badRequest('Error in GET'); }); }, 
0
source share

All Articles