This question is very similar to How to disable Express BodyParser for file uploads (Node.js). But the answer they have provided is for Express and I have tried the solution with the Restify 7, and it does not seem to work.
I'm using Node.js + Restify to build Restfull application. I am using BodyParser,to parse post parameters. However, I would like to have access to multipart form-data POSTS.
I use multer, multer-s3 and aws-sdk and I want to have access to the data to send them to digitalocean sapce. But all file uploads are parsed automatically by restify.plugin.bodyParser.
Is there a way for me to disable the BodyParser for multipart formdata posts without disabling it for everything else?
This is an example code :
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, res, async (error) => {
if (error) {
console.log(error);
return next(new errors.InvalidContentError(error));
}
console.log('File uploaded successfully.');
res.send(200);
next();
});
});