0

在 Sails.js 0.10.5 中,我想将 bodyParser 替换为特定路径。例如,对路径“/app/upload”使用不同的正文解析器,其余的使用默认值。我该怎么做呢?

4

1 回答 1

1

您可以通过覆盖来做到这一点config/http.js。将您的自定义解析器添加到中间件,并按bodyParser顺序替换为您的自定义解析器。

像这样的东西应该工作

module.exports.http = {
    middleware: {
        superBodyParser: function (req, res, next) {
            if (req.path === '/app/upload') {
                // your custom parser
            }
            else {
                require('skipper')(req, res, next);
            }
        },
        order: [
          'startRequestTimer',
          'cookieParser',
          'session',
          'myRequestLogger',
          // 'bodyParser',  <-- not required anymore
          'superBodyParser' 
          'handleBodyParserError',
          'compress',
          'methodOverride',
          'poweredBy',
          '$custom',
          'router',
          'www',
          'favicon',
          '404',
          '500'
        ]
    }
};
于 2016-01-13T07:25:53.827 回答