将 express-busboy 限制在特定路线
Limit express-busboy to specific routes
我使用存储库中的示例使用 express-busboy 设置文件上传 here
它似乎没有使用正常的 use()
语法,所以我对如何实际限制这个中间件有点困惑,所以它只在特定路由上执行,因为它破坏了其他 POST 请求。
我是这样配置的:
var busboy = require('express-busboy');
busboy.extend(app, {
upload: true,
path: './uploads/temp'
});
在 allowedPath 值中,您可以在这种情况下指定正则表达式限制在 express 应用程序中定义的 post 路由。喜欢/上传
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);
好吧,因为 express-busboy 对我不起作用,所以我尝试使用 express-fileupload 代替,现在似乎可以用了。
尝试使用 Multer,并将其限制在您的路线中:
app.post('/^\/api\/ccUpload$/',
multer({
dest: './uploads/temp',
rename: function(fieldname, filename, req, res) {
return filename.toLowerCase();
}
}),
yourRouteHandler
);
我使用存储库中的示例使用 express-busboy 设置文件上传 here
它似乎没有使用正常的 use()
语法,所以我对如何实际限制这个中间件有点困惑,所以它只在特定路由上执行,因为它破坏了其他 POST 请求。
我是这样配置的:
var busboy = require('express-busboy');
busboy.extend(app, {
upload: true,
path: './uploads/temp'
});
在 allowedPath 值中,您可以在这种情况下指定正则表达式限制在 express 应用程序中定义的 post 路由。喜欢/上传
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);
好吧,因为 express-busboy 对我不起作用,所以我尝试使用 express-fileupload 代替,现在似乎可以用了。
尝试使用 Multer,并将其限制在您的路线中:
app.post('/^\/api\/ccUpload$/',
multer({
dest: './uploads/temp',
rename: function(fieldname, filename, req, res) {
return filename.toLowerCase();
}
}),
yourRouteHandler
);