Multer: CLS Context is lost after using multer middleware

Created on 12 Dec 2019  路  2Comments  路  Source: expressjs/multer

Hello! I'm using the cls-hooked library, which let's me use continuous local storage in my express app. After I use the multer.file middleware, my context is lost.

I patched the middleware using the solution proposed here and now it works fine. I'm letting you know just in case you want to fix it.

Cheers,

Additional info
Node version: 10.15.3
Express version: 4.16.3
Multer version: 1.4.2
CLS Hooked version: 4.2.2
OS: Ubuntu 18.04.3 LTS

Most helpful comment

I struggled for a long time with the same issue, but I was able to go around it by passing all necessary middlewares with the following notation:

async function uploadMiddleware (req: Request, res: Response, next: NextFunction) {
    try {
        await new Promise<void>((resolve, reject) => {
            multer({
                // ...
            }).array('files')(req, res, (error: any) => {
                if (error) return reject(error);

                return resolve();
            });
        });

        next();
    } catch (error) {
        next(error);
    }
}

server.get('/endpoint', uploadMiddleware, controller);

So basically what I'm doing is, I'm running the multer middleware in-directly by wrapping it in another middleware that handles context correctly and then, at correct time, continuing to next middlewares/controller by calling next().

Edit: just noticed it's basically the same solution as in the aforementioned medium.com article. So... yay me :) having it directly here may help people stumbling here on this problem.

All 2 comments

Hello,

I have the same issue with the object AsyncLocalStorage (from 'async_hooks'), the context is lost on the route which has the multer middleware.

Edit: https://medium.com/@theekshanawj/nodejs-using-multer-and-cls-hooked-together-a00decbebab6 fixes my problem ^^

I struggled for a long time with the same issue, but I was able to go around it by passing all necessary middlewares with the following notation:

async function uploadMiddleware (req: Request, res: Response, next: NextFunction) {
    try {
        await new Promise<void>((resolve, reject) => {
            multer({
                // ...
            }).array('files')(req, res, (error: any) => {
                if (error) return reject(error);

                return resolve();
            });
        });

        next();
    } catch (error) {
        next(error);
    }
}

server.get('/endpoint', uploadMiddleware, controller);

So basically what I'm doing is, I'm running the multer middleware in-directly by wrapping it in another middleware that handles context correctly and then, at correct time, continuing to next middlewares/controller by calling next().

Edit: just noticed it's basically the same solution as in the aforementioned medium.com article. So... yay me :) having it directly here may help people stumbling here on this problem.

Was this page helpful?
0 / 5 - 0 ratings