限制快递到特定路线

问题描述 投票:4回答:3

我使用来自存储库here的示例使用express-busboy设置文件上传,其中似乎没有使用正常的use()语法,所以我对如何实际限制此中间件以使其仅在特定路由上执行感到困惑因为它打破了其他POST请求。

这是我配置它的方式:

var busboy = require('express-busboy');

busboy.extend(app, {
    upload: true,
    path: './uploads/temp'
});
node.js express file-upload middleware busboy
3个回答
0
投票

在allowedPath值中,您可以在明确应用程序中定义的后路由中指定此案例限制中的正则表达式。喜欢/上传

busboy.extend(app, {
    upload: true,
    path: './uploads/temp',
    allowedPath: /^\/uploads$/

});

或者其他方面你可以通过功能

var options = {
        upload: true,
        path: './uploads/temp',


    };
options.allowedPath = function(url) {
    return url == '/api/ccUpload';
}

    busboy.extend(app, options);

0
投票

好吧,因为快递busboy不适合我,我尝试使用express-fileupload而现在似乎工作。


0
投票

尝试使用Multer,并将其限制在您的路线:

app.post('/^\/api\/ccUpload$/',
  multer({
    dest: './uploads/temp',
    rename: function(fieldname, filename, req, res) {
      return filename.toLowerCase();
    }
  }),
  yourRouteHandler
);
© www.soinside.com 2019 - 2024. All rights reserved.