question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ignore routes for serve-static on api endpoints

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
darrenjenningscommented, Feb 24, 2020

@kamilmysliwiec I was able to get it to work with:

exclude: ['/api/(.*)'],

I looked at the regex returned from the path-to-regexp in the isRouteExcluded util. The latest version of the library (https://forbeslindesay.github.io/express-route-tester/) returns this regexp:

// '/api*'
^\/api\/(.*)\/?(?=\/|$)

but the code in my node_modules is returning which seems to disregard that a wildcard needs to be transformed:

// '/api*'
^\/api\/\*(?:\/(?=$))?$

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.

0reactions
kamilmysliwieccommented, Feb 23, 2020
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found