static
static copied to clipboard
Folders are not redirected if missing a slash at the end
Expected behavior
If the requested URL points to a folder with an index.html file and it is missing a slash at the end, the server should respond with a redirect with a slash added at the end.
Actual behavior
If the requested URL points to a folder with an index.html file and it is missing a slash at the end, the server does not redirect with the slash added, making the browser think that the folder name is a file name, which in turn makes relative URLs work differently depending on whether or not the slash is present.
How to reproduce
- Download and unpack this tiny project: koa-static-folder-bug.zip
npm installandnpm start- Open
http://localhost:3000/folder. Observe that no redirect took place. The image of a black square is displayed, even thoughimage.pngis in the parent directory offolder/index.html. - Open
http://localhost:3000/folder/. Observe that the image is (correctly) not displayed.
I support either resolving this issue with the expected behavior or an evaluation of the expected behavior.
I spent more time than I want to admit on a wild goose change, because I did not notice the missing slash in my URLs. But, I propose an alternate expected behavior.
Expected behavior
If the requested URL points to a folder with an index.html file and it is missing a slash at the end, the server should respond with a 404, not the index.html file (because folder describes a file—a different file than index.html).
Either, help the user out completely (my interpretation of the suggestion of the original poster); or, do not halfway help the user, thus leading to unexpected behavior.
Hi, do you plan to implement the expected behaviour? If not, I'll need to implement a workaround requiring me hitting the disk for every incoming request to test whether the requested resource is a directory or not. This is obviously sub-optimal since downstream, koa-send will again stat the filesystem for each request.
i thought this was an option...
I think this solved by this PR.