-
-
Notifications
You must be signed in to change notification settings - Fork 962
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
NotFound ignored when FileServer using "/" path #155
Comments
good question. I believe r.FileServer("/") will make a route on /* which catches every request to its handler. Potentially r.FileServer() impl could be changed to call r.NotFound() if a file is not found. You do have the option to write the file serving handler yourself, the r.FileServer() is just a helper method. Please let me know if you solve it. |
@pkieltyka locally, I just updated the mx.Get() call in r.FileServer() to check to see if the file exists before serving. It may not be the most efficient solution, but it's working as it should...
Any interest in a pull request for it? (I would include tests) |
thanks for letting me know, but not at this time. But that should let you keep going, just take your own FileServer function to register the handler |
Cool. Here's my solution for a FileServer function that incorporates the pressly/chi NotFoundHandler for anyone that's interested...
Thanks @pkieltyka |
thanks @jsadwith |
Just as a comment you would probably want to parse r.RequestURI as it will include the query string as well which you probably don't want when stating the file system |
Since this came up with trying out the embed.FS, we discovered this same error (nginx had handled it with a The above solution seems to basically be the same thing as the nginx rule and that worked for us when applied to the http.FS. It can be done with the embed.FS as well, but the method changes and is less portable since the http.FS can operate on both. with a file layout of
and the above, adapted:
you get a static webserver that doesn't 404 when a user tries to navigate to a view on the UI that the api server/web server isn't aware of. |
When using FileServer with a root ("/") path, the NotFound method seems to get ignored...
An unknown route will always return the default "404 page not found" rather than the expected "nothing here". Changing the FileServer path to anything else (ie. "/foo") produces the expected "nothing here".
The goal is to be able to serve an Angular frontend at the root path, while all API routes are served via "/api/v1/". I also want to route any 404 traffic to the root to be able to better handle Angular routing if a user refreshes the page while within a frontend route.
Is the above an expected result or a bug? If expected, what would be the best way to go about doing what I'm looking for?
The text was updated successfully, but these errors were encountered: