Open pacyL2K19 opened 7 months ago
You need to change the req.url
https://stackoverflow.com/questions/70822989/how-to-rewrite-url-path-in-nestjs
You need to change the
req.url
https://stackoverflow.com/questions/70822989/how-to-rewrite-url-path-in-nestjs
Thank you for the workaround This works as well for me 🎉
However, we still have an issue here in the response
Since the API has handlers for both GET v1/ and GET v2/, these responses are misleading
{
"message": "Cannot GET /v2",
"error": "Not Found",
"statusCode": 404
}
{
"message": "Cannot GET /v1/",
"error": "Not Found",
"statusCode": 404
}
I think it should say something different, not that it can't handle them @micalevisk
Is there an existing issue for this?
Current behavior
I wanted to add versioning support to an existing Nest.js REST API. Previously, all the routes were handled without version in the URL (like a GET at
/users
) and now I want:v1
routes, means if I perform a GET at/users/
this needs to be handled by/v1/users
handlerMy solution was to:
async function bootstrap() { const app = await NestFactory.create(AppModule); app.enableVersioning({ type: VersioningType.URI, defaultVersion: '1', // all routes without version prefix will be treated as v1 default prefix being
v
}); await app.listen(3000); } bootstrap();@Version('2') @Get() getHelloV2(): string { return this.appService.getHelloV2(); }
The purpose of the
VersionManagementMiddleware
is to ensure that all incoming requests are automatically directed to the appropriate version of our API based on the URL path. Specifically, it aims to:It's consumed in the app as following:
With this in place, i expected all the requests missing the version to be handled as v1, all with wrong version to be handled as v2 and everything else as expected, means a GET at
v1/
or/v2
should be ignored by the middleware, but this is what currently happens:When versions exist in the request -> everything works as expected -- see the screenshot below
When missing the version, it recognizes I am trying to hit the v1/ but sends a 404 error while the v1/ handler exist
When using a bad version, it recognizes I am trying to hit the v2/ but still send a 404 while v2/ handler also exists
Minimum reproduction code
https://github.com/pacyL2K19/request-original-url-mutation-middleware
Steps to reproduce
Expected behavior
I expected the middleware to mutate the
request.originalUrl
so that requests without version are handled by v1 and those with bad version to be handled by v2 that way, my clients won't have to change anything on their side, this means the app can still work with endpoints missing version(or with unsupported versions) and the ones with versionPackage
Other package
express
NestJS version
10.3.2
Packages versions
Node.js version
20.11.0
In which operating systems have you tested?
Other
No response