superstatic
superstatic copied to clipboard
Fix redirect as middleware
Regarding the following error:
- You mount superstatic as a middleware on a path, instead of on no path (root), for example:
app.use('/basepath', superstatic())instead ofapp.use(superstatic()). - You make a request that superstatic is configured to redirect, such as
/basepath/something/index.html→/basepath/something. - The error was that superstatic did not take
/basepathinto account when redirecting, and would redirect only to the path below/basepath. In the example above, that would be to/something. When the client makes that new request to/something, express or connect responds404because superstatic is not mounted on that path.
This PR fixes that, by keeping req.url and req.originalUrl specifically apart for the purpose of redirects.
-
req.urlcontaining information needed for mapping to a real file within thepublicdirectory, and -
req.originalUrlcontaining information about how the request was actually made by the client. This is used for calculating correct redirects.
I updated files.spec.ts to run all its static file tests both with superstatic mounted as before, and as a middleware mounted under a base path.
Rebased this fix on top of master. Apparently needs approval for PR build.