Reputation: 353
I'm new to sails js and still exploring the framework. My question is that, can we bind an action inside a controller whenever there is a bad request from front end? rather than calling in the response page, the customized controller is instead called.
Please shed some light. Thanks
Upvotes: 1
Views: 703
Reputation: 24948
You have a few options:
res.badRequest()
, which will return a 400 status response to the browser.You can create a service to house custom "bad request" response code, and call it from any controller. For example, make a api/services/ResponseService.js file with a method like:
badRequest: function(req, res) {
return res.send(400, "You've been bad!");
}
and call it from your controller like ResponseService.badRequest(req, res);
badRequest
response by modifying api/responses/badRequest.js. This has all the benefits of #2, while allowing you to use the semantics of #1 (i.e. just calling res.badRequest()
) and avoiding the overhead of a separate service module.Upvotes: 1