sails.js
컨트롤러
수색…
비고
컨트롤러 ( MVC 의 C )는 웹 브라우저, 모바일 응용 프로그램 또는 서버와 통신 할 수있는 다른 시스템의 요청에 응답하는 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