Search code examples
node.jsexpressmulterexpress-jwt

req.files is always null express-fileupload


TLDR : express-jwt creates conflicts with packages adding stuff in the req parameter of the node.js API (express-upload, multer for example).

I'm working on a node.js API and I can't remove any package. I'm trying to upload files from the front end to the API. I used many packages for that (express-fileupload, multer ...) but none of them fixed my problem. The person who created the API used express-jwt for authentification. It works by adding some information in the req.user field on the API.

My problem is that the packages used to upload files in nodejs seems to create conflicts with express-jwt. If i disable it, req.files is populated as it should and everything works perfectly. But when i reinstall express-jwt, req.files is always null. I can't just use another authentification package. I have no idea to solve this problem.


Solution

  • I figured it out. You have to call the express-fileupload middleware first. I added the following code and req.files was accessible in any middleware called after that.

    const fileUpload = require('express-fileupload');
    app.use(fileUpload());
    app.use((req: any, res: any, next: any) => {
        console.log("user :  " + req.user + " ===== files : " + req.files);
        next();
    })
    

    For any reason if you call express-jwt first, req.files will not be populated. For me the line that caused the conflict was this one :

    app.use(expressJwt({secret: PUBLIC, strict : false}).unless({path : unless}));