ignore routes for serve-static on api endpoints
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Currently if you serveStatic alongside api routes, it would be nice if you could tell serve-static to ignore paths beginning with /api
via some options. This would allow /api/not-found
to return 404 from API, and not try and serve an index.html file. If there is some current way to do it, I’m also curious how I might get around this with middleware if necessary.
Expected behavior
The /api
routes do not serve static on 404s.
Minimal reproduction of the problem with instructions
Run a boilerplate nestjs app with serveStatic. add app.setGlobalPrefix('api')
and then try and hit /api/not-found-route-goes-here
and it will serve the index.html
.
What is the motivation / use case for changing the behavior?
monorepo api + webapp
Environment
Nest version: 6.12.9
For Tooling issues:
- Node version: v12.13.0
- Platform: Mac
Others:
N/A
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:8 (3 by maintainers)
Top Results From Across the Web
How can I exclude some routes from ... - Stack Overflow
I need to use app.UseStatusCodePagesWithReExecute("/"); in my Configure() method to redirect any unknown requests to index.html , as routing is ...
Read more >Serve static | NestJS - A progressive Node.js framework
You can set the path to render your static app, specify excluded paths, enable or disable setting Cache-Control response header, etc. See the...
Read more >hapi — Ignore Trailing Slashes on Route Paths - Future Studio
In the following, you'll learn how to properly handle request URLs having a trailing slash. hapi Series Overview. hapi; Futureflix; Futureflix ...
Read more >Node.js Handling invalid routes - GeeksforGeeks
It can be done by writing a custom route or redirecting all the invalid routes to any custom page. Let's develop an simple...
Read more >Handling SPA Fallback Paths in a Generic ASP.NET Core ...
For most ASP.NET Core MVC, Razor or plain API applications, the endpoints.MapFallbackXXX() are probably sufficient to provide fallback route ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kamilmysliwiec I was able to get it to work with:
exclude: ['/api/(.*)'],
I looked at the regex returned from the
path-to-regexp
in theisRouteExcluded
util. The latest version of the library (https://forbeslindesay.github.io/express-route-tester/) returns this regexp:but the code in my node_modules is returning which seems to disregard that a wildcard needs to be transformed:
So it could be some dependency version resolving to an older version as it seems my lockfile has many versions of path-to-regexp. Looks my local is resolving to
0.2.5
.Follow this example https://github.com/nestjs/nest/tree/master/sample/24-serve-static (it works here)