admin管理员组文章数量:1291279
I'm looking for a way to use the default 404 error provided by sails.js framework.
The doc is here !documentation/config.404 but I'm wondering how I can call the 404 method from another controller.
Of course I could use the code in the doc, but I would have prefered to use the dedicated framework method.
I'm looking for a way to use the default 404 error provided by sails.js framework.
The doc is here http://sailsjs/#!documentation/config.404 but I'm wondering how I can call the 404 method from another controller.
Of course I could use the code in the doc, but I would have prefered to use the dedicated framework method.
Share Improve this question edited Nov 4, 2015 at 19:08 Joe Hill 3333 silver badges12 bronze badges asked Feb 2, 2014 at 20:01 VadorequestVadorequest 18.1k27 gold badges129 silver badges231 bronze badges2 Answers
Reset to default 10res.notFound()
should do the trick for version 0.10.
Have a look at your api/responses/
folder, it contains the default error response helpers for sails and allows you to e up with your own response types by saving files there.
Included by default:
/api/responses
badRequest.js - 400 - res.badRequest()
notFound.js - 404 - res.notFound()
forbidden.js - 403 - res.forbidden()
serverError.js - 500 - res.serverError()
Roll your own:
/api/responses
notAcceptable.js - res.notAcceptable();
Example (modified api/responses/notFound.js):
module.exports = function notAcceptable() {
var req = this.req;
var res = this.res;
var viewFilePath = 405;
var statusCode = 405;
var result = {
status: statusCode
};
if (req.wantsJSON) {
return res.json(result, result.status);
}
res.status(result.status);
res.render(viewFilePath, function(err) {
if (err) {
return res.json(result, result.status);
}
res.render(viewFilePath);
});
};
The pageNotFound
method, described in documentation is just default implementation of 404 responding behavior and is part of sails config. You can see it in config/404.js file.
If you want to call it from your controller, you can refer to it the same way as you refer any other sails config variables:
yourControllerAction: function(req, res) {
......
// if content not found, calling 404 method
sails.config[404](req, res);
// else
res.send('Some content');
},
本文标签: javascriptsailsjs call 404 manuallyStack Overflow
版权声明:本文标题:javascript - sails.js: call 404 manually - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507730a2382422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论