Search code examples
node.jstypescriptbusboy

Type 'IncomingHttpHeaders' is not assignable to type 'BusboyHeaders'


I am using busboy in typescript/Node project for file uploading, In every documentation of busboy they initialize it with request headers, but I'm getting this error Type 'IncomingHttpHeaders' is not assignable to type 'BusboyHeaders'. here is my code

import { NextFunction, Request, Response, Router } from "express";
import Busboy = require('busboy');
import * as path from "path";
import * as fs from "fs";

export class FileUploader {

    // method for file upload to server.
    public upload(req: Request, res: Response, next?: NextFunction) {
        const busboy = Busboy({ headers: req.headers });
        

        busboy.on("file", function (fieldname, file, filename, encoding, mimetype) {

            // path to file upload
            const saveTo = path.join((path.join(__dirname, "/../images/" + filename)));

            file.pipe(fs.createWriteStream(saveTo));
        });

        busboy.on("finish", function () {
            res.status(200).json({ "message": "File uploaded successfully." });
        });
        req.pipe(busboy);

    }
}

getting error on the following line

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

Solution

  • Busboy just requires content-type (lowercase) as the header field. Just provide the express request content type for busboy:

    import * as Busboy from 'busboy';
    
        const busboy = new Busboy({
            headers: {
                'content-type': event.headers['content-type'] || event.headers['Content-Type'],
            }
        });