Edit Page

Generating controllers or standalone actions

You can use sails-generate from the Sails command line tool to quickly generate a controller, or even just an individual action.

Generating controllers

#

For example, to generate a controller:

$ sails generate controller user

Sails will generate api/controllers/UserController.js:

/**
 * UserController.js
 *
 * @description :: Server-side controller action for managing users.
 * @help        :: See https://sailsjs.com/documentation/concepts/controllers
 */
module.exports = {

}

Generating standalone actions

#

Run the following command to generate a standalone action:

$ sails generate action user/signup
info: Created an action!
Using "actions2"...
[?] https://sailsjs.com/docs/concepts/actions

Sails will create api/controllers/user/sign-up.js:

/**
 * user/sign-up.js
 *
 * @description :: Server-side controller action for handling incoming requests.
 * @help        :: See https://sailsjs.com/documentation/concepts/controllers
 */
module.exports = {


  friendlyName: 'Sign up',


  description: '',


  inputs: {

  },


  exits: {

  },


  fn: function (inputs, exits) {

    return exits.success();

  }


};

Or, using the classic actions interface:

$ sails generate action user/signup --no-actions2
info: Created a traditional (req,res) controller action, but as a standalone file

Sails will create api/controllers/user/sign-up.js:

/**
 * Module dependencies
 */

// ...


/**
 * user/signup.js
 *
 * Signup user.
 */
module.exports = function signup(req, res) {

  sails.log.debug('TODO: implement');
  return res.ok();

};

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Concepts