2

I got a basic question.

I'm trying out Sails (http://sailsjs.org/) and it has terminal commands to generate entity such as User entity:

sails generate api user

My question is, the UserController.js file shows:

/**
 * UserController
 *
 * @description :: Server-side logic for managing users
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {

};

How come when I access:

http://localhost:1337/user/create

It knows how to create a new User entity ? The controller clearly does not have a create action like this:

module.exports = {
    create: function(req, res) {
        // code to create new user
    }
};

So surely nothing should happen.

I did a bit of Symphony 2.0 PHP web framework and we needed to create those actions manually.

I'm confuzzled and impressed at the same time, any ideas ?

Zhang
  • 11,549
  • 7
  • 57
  • 87

1 Answers1

3

Welcome to the Sails.js world!

You have just discovered the Blueprint API.

When you lift your app, Sails will add generic actions to your controllers that have a model of the same name (to this day find, findOne, create, update, destroy, populate, add and remove actions exist implicitly). That's called Blueprints actions.

In addition, Blueprints routes can also be binded to your controllers' actions. Here is the list of those routes:

  • Blueprints RESTful routes: automatically generated routes to expose a conventionnal REST API on top of find, create, update, and destroy actions
    • GET /post -> PostController.find
    • GET /post/:id -> PostController.findOne
    • POST /post -> PostController.create
    • PUT /post/:id -> PostController.update
    • DELETE /post/:id -> PostController.destroy
  • Blueprints shortcuts routes: simple helpers to provide access to a controller's CRUD methods from your browser's URL bar
    • GET /user/create?name=joe -> Post.create
    • GET /user/update/1?name=mike -> Post.update
    • GET /user/destroy/1 -> Post.destroy
  • Blueprints actions routes: automatically create routes for your custom controller actions
    • GET /group/count -> Post.count

Each of them can be deactivated in the config/blueprints.js file.

You can find more details on the docs.

Check this SO question if you want to redefine the blueprints actions.

Community
  • 1
  • 1
Yann Bertrand
  • 3,084
  • 1
  • 22
  • 38
  • I'm writing a little project [on GitHub](https://github.com/YannBertrand/SailsBasicViewsProject) if you want to take full advantage of the Blueprint API power – Yann Bertrand Jul 03 '15 at 21:42