您可以使用 Sails 命令行工具中的 sails-generate
快速生成控制器,甚至只生成单个操作。
例如,要生成一个控制器
$ sails generate controller user
Sails 将生成 api/controllers/UserController.js
/**
* UserController.js
*
* @description :: Server-side controller action for managing users.
* @help :: See https://sails.js.cn/documentation/concepts/controllers
*/
module.exports = {
}
运行以下命令生成一个独立操作
$ sails generate action user/signup
info: Created an action!
Using "actions2"...
[?] https://sails.js.cn/docs/concepts/actions
Sails 将创建 api/controllers/user/sign-up.js
/**
* user/sign-up.js
*
* @description :: Server-side controller action for handling incoming requests.
* @help :: See https://sails.js.cn/documentation/concepts/controllers
*/
module.exports = {
friendlyName: 'Sign up',
description: '',
inputs: {
},
exits: {
},
fn: function (inputs, exits) {
return exits.success();
}
};
或者,使用 经典操作 接口
$ sails generate action user/signup --no-actions2
info: Created a traditional (req,res) controller action, but as a standalone file
Sails 将创建 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();
};