sails.js
コントローラ
サーチ…
備考
コントローラ( MVCのC )は、Webブラウザ、モバイルアプリケーション、またはサーバーと通信できる他のシステムからの要求に応答するSailsアプリケーションの主なオブジェクトです。彼らはしばしばあなたのモデルとビューの仲介者として行動します。多くのアプリケーションでは、コントローラーにはプロジェクトのビジネスロジックの大部分が含まれます。
ES2015構文
'use strict';
// This is an example of a /api/controllers/HomeController.js
module.exports = {
// This is the index action and the route is mapped via /config/routes.js
index(req, res) {
// Return a view without view model data
// This typically will return the view defined at /views/home/index.<view engine extension>
return res.view();
},
foo(req, res) {
// Return the 'foo' view with a view model that has a `bar` variable set to the query string variable `foobar`
return res.view({
bar: req.param('foobar'),
});
},
};
co.jsでES2015ジェネレータを使用する
'use strict';
const co = require('co');
module.exports = {
// This is the index action and the route is mapped via /config/routes.js
index(req, res) {
co(function* index() {
// Return a view without view model data
// This typically will return the view defined at /views/home/index.<view engine extension>
return res.view();
}).catch(res.negotiate); // Catch any thrown errors and pass the error to the `negotiate` policy.
},
foo(req, res) {
co(function* foo() {
// Get an array of `FooBar` items from the database
const items = yield FooBar.find();
// Return the 'foo' view with a view model containing the array of `FooBar` items
return res.view({
items,
});
}).catch(res.negotiate); // Catch any thrown errors and pass the error to the `negotiate` policy.
},
};
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow