微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为文件上传禁用 restify.plugin.bodyParser

如何解决如何为文件上传禁用 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?