-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question or Feature Request] 404 error handling or sendFile on non existed route #298
Comments
You can use an interceptor to check if a response was sent back, and if it wasn't, send the error. |
@wbhob Main reason is to sendFile for all not existed routes. |
There isn't a good 'nest way' to do this because it maps all the URLs before the requests are instantiated, so there isn't an apparent hierarchy. You could try to maybe make a controller with the route |
How I can do it as very last controller? |
Just make it the last one in your controllers array |
@wbhob hope, I understand you, but I use modules. Each Module contain routes for example: I test your solution, and it works, but because I use modules, I need dedicated module and controller for it for using it as last module in app.module modules array. Thanks. Will be good if someone add that solution to documentation because too many people want to use Nest for API and also serve frontend on all unused routes (for example angular). |
Hi @pumano, |
This is not the place for this type of question @ToporDon. please open a new issue, or better yet, as on Gitter. |
@kamilmysliwiec good idea! Thank you! Also will be good if you add docs (or I can help with it via pull request) about that NotFoundException and examples about how to catch NotFoundException and how to serve frontend via |
Hi @pumano, |
If someone interested about how to do it. not-found-exception.filter.ts:
server.ts
|
Thanks for sharing @pumano! 🙂 |
Hi, @pumano, do you use app.setGlobalPrefix? When I use it, this solution doesnt work, but without global prefix its works |
@silvelo, no, I don't use app.setGlobalPrefix. maybe it's bug? |
Yes, maybe global configs make that issue. |
Actually, it's not a bug. The |
So at the moment I have two options:
Because my NestApplication doesnt response to any url wihtout that prefix. Thanks @kamilmysliwiec |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm submitting a...
Current behavior
How to handle 404 when route not existed or how to sendFile on non existed routes?
Expected behavior
I want to handle 404 not found properly. In pure express application I just add (after all routes):
and it's works for all routes except previously defined by other routes. When I request non defined route I got 'not found'.
Minimal reproduction of the problem with instructions
I found a solution how to implement what I need, but it's not a 'Nest' way:
What is the motivation / use case for changing the behavior?
I want to implement that in Nest Way. I want to possibility to create some controller with '*' which can be loaded as latest route controller for catching all non existed requests for handling 404 or for sendFile aka static for frontend:
Environment
The text was updated successfully, but these errors were encountered: