Not able to update req url since req.raw is undefined in nestjs fastify custom middleware

498 views Asked by At

I have applied middleware to one of my application in Nestjs Fastify. In middleware I want to access userId from req url and remove it from there. Since I was using FastifyRequest as a req type I was not able to reassign req.url.

I got to know that I have to use req.raw in such case but req.raw inside middleware class is undefined for me.

Even I found req.originalUrl and I tried to update it but with changed originalUrl I am getting req url not found exception.

AbcMiddleware.ts

const token = matches[0].indexOf('api') > -1 ? matches[1] : matches[0];
//(not able to reassign req.url since it is readonly property)
req.url = req.originalUrl.replace(`${token}/`, '');
//(I am getting req url not found exception with originalUrl)
req.originalUrl = req.originalUrl.replace(`${token}/`, '');
//(req.raw is undefined)
req.raw.url= req.originalUrl.replace(`${token}/`, '');
0

There are 0 answers