0

Tried it using these codes but the busboy.on('file') is not triggered, I was not able to see console.log() inside busboy.on('file'), The upload is working but I want to make sure that the code inside 'file' is working and triggered

const Busboy = require('busboy');
const AWS = require('aws-sdk');
const config = require('../../config/config');
const path = require('path');
const os = require('os');
const fs = require('fs');

const s3 = new AWS.S3();

function uploadToS3(file, params) {

const fileName = file.name;
let getParams = {
    Bucket: `${config.amazon.s3.bucketName}/uploads/${params.refType}/${params.refId}`,
    Key: fileName,
    Body: file.data
}

s3.upload(getParams, function (err, data) {
    if (err) {
        console.log('error in callback')
        console.log(err)
    } else {
        console.log('success');
        console.log(data)
    }
})

}

module.exports = {
upload : function(req, res, next) {

    let busboy = new Busboy({ headers: req.headers });

    busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
        file.on('data', function (data) {
            console.log(`File ${fieldname} got ${data.length} bytes`)
        })
        file.on('end', function () {
            console.log(`File ${fieldname} finished`)
        })
        let saveTo = path.join(os.tmpdir(), path.basename(fieldname))
        file.pipe(fs.createWriteStream(saveTo));
        console.log(saveTo);
    })

    busboy.on('finish', function() {
        console.log('upload finished');
        const file = req.files.element2;
        uploadToS3(file, req.params)
    })

    req.pipe(busboy)
}

}

index.js

const busboy = require('connect-busboy');
app.use(busboy());

Content-Type is multipart/form-data

here is the postman request

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.