如何解决如何为文件上传禁用 restify.plugin.bodyParser
这个问题与 How to disable Express BodyParser for file uploads (Node.js). 非常相似,但他们提供的答案是针对 Express 的,我已经尝试过使用 Restify 7 的解决方案,但似乎不起作用。
我正在使用 Node.js + Restify 来构建 Restfull 应用程序。我正在使用 BodyParser 来解析帖子参数。但是,我想访问多部分表单数据帖子。
我使用 multer、multer-s3 和 aws-sdk,我想访问数据以将它们发送到 digitalocean sapce。但是所有上传的文件都会被restify.plugin.bodyParser自动解析。
有没有办法为多部分表单数据帖子禁用 BodyParser,而不为其他所有内容禁用它?
const restify = require('restify');
const errors = require('restify-errors');
const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const server = restify.createServer();
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.bodyParser());
// Set S3 endpoint to DigitalOcean Spaces
const spacesEndpoint = new aws.Endpoint('nyc3.digitaloceanspaces.com');
const s3 = new aws.S3({
endpoint: spacesEndpoint
});
const upload = multer({
storage: multerS3({
s3: s3,bucket: 'space_name',acl: 'public-read',key: function (request,file,cb) {
console.log(file);
cb(null,new Date().toISOString());
}
})
}).single('logo');
server.post('/upload',async (req,res,next) => {
upload(req,async (error) => {
if (error) {
console.log(error);
return next(new errors.InvalidContentError(error));
}
console.log('File uploaded successfully.');
res.send(200);
next();
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。