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 badges
Add a ment  | 

2 Answers 2

Reset to default 10

res.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