Gaui
Gaui

Reputation: 8929

Specify routes and verbs above controller actions

Is it possible to specify routes and accepted verbs above controller actions, without having to have to specify it in config/routes.js ?

module.exports = {
    // GET /users
    getAllUsers: function (req, res) {
        User.find().exec(function (err, obj) {
            res.send(obj);
        });
    },
    // GET /users/:id
    getUserByID: function (req, res) {
        User.find({id:req.params.id}).exec(function (err, obj) {
            res.send(obj);
        });
    },
    // POST /users
    createUser: function (req, res) {
        User.create(req.body).exec(function (err, obj) {
            res.send(201, obj);
        });
    }
};

Upvotes: 1

Views: 326

Answers (1)

sgress454
sgress454

Reputation: 24948

No, this is not currently supported by Sails. However, if you're just trying to create RESTful routes for a model, Sails gives you those for free via the blueprints system. So by default, if you have a api/models/User.js file and a api/controllers/UserController.js file, then the following routes are added automatically:

GET    /user     ==> UserController.find
GET    /user/:id ==> UserController.findOne
POST   /user     ==> UserController.create
PUT    /user/:id ==> UserController.update
DELETE /user/:id ==> UserController.destroy

There are default handlers for these actions, or you can override them in your controller file.

For models with associations to other models, some extra routes are also added. See the full blueprint API reference for details.

Note that you can also bind route handlers directly in the config/routes.js file, e.g. "get /foo": function(req, res) {...}, but that sorta defeats the purpose of using an MVC framework.

Upvotes: 4

Related Questions