Serve: Name of folder without trailing slash serves files incorrectly

Created on 3 Jul 2018  路  4Comments  路  Source: vercel/serve

I have a folder books with multiple sub-folders. Each contains a compiled Gitbook (static HTML, JS, CSS). There is a navigation which allows you to switch between these books / sub-folders.

  • books/

    • Manual/

    • index.html

    • styles/



      • style.css



    • ...

    • Drivers/

    • .../

  • index.html

The top-level index.html contains the following redirect:

<html><head><meta http-equiv="refresh" content="0; url=Manual/"></head><body></body></html>

Using serve books with version 9.1.1 and Node.js 10.5.0, the switching does not work as expected:

Opening http://localhost:5000 in a browser leads to http://localhost:5000/Manual (note the missing slash at the end) and everything works correctly. However, if I click e.g. _Drivers_ in the navigation, some JS code tries to put me on http://localhost:5000/Drivers, but only the HTML is rendered, without any styles. This is because the CSS is searched in the root location (books) instead of the current folder (books/Drivers).

If I request http://localhost:5000/Drivers/ (note the trailing slash) then everything is served as expected.

Serve should redirect from /Drivers to /Drivers/ if it serves /Drivers/index.html IMO.

There were no such issues in older versions of serve. It worked with /Drivers as well. It started to become a problem with an upgrade to Node.js 10.5.0 (from 6.7.0) together with an upgrade of serve, which had an incompatible latest version for 6.x, so that's why I did not upgrade serve for a while. I can't remember the outdated version I used before which didn't have this problem unfortunately.

Most helpful comment

Also, the automatic removal of /index.html e.g. from http://localhost:5000/HTTP/General/index.html to http://localhost:5000/HTTP/General breaks relative paths too.

curl -vv http://localhost:5000/HTTP/General/index.html
...
< HTTP/1.1 301 Moved Permanently
< Location: /HTTP/General/index
...
curl -vv http://localhost:5000/HTTP/General/index
...
< HTTP/1.1 301 Moved Permanently
< Location: /HTTP/General

Using serve 9.2.0

All 4 comments

I think so, it's a de facto standard for web servers to redirect /drivers to /drivers/ when serving index.html.

Also, the automatic removal of /index.html e.g. from http://localhost:5000/HTTP/General/index.html to http://localhost:5000/HTTP/General breaks relative paths too.

curl -vv http://localhost:5000/HTTP/General/index.html
...
< HTTP/1.1 301 Moved Permanently
< Location: /HTTP/General/index
...
curl -vv http://localhost:5000/HTTP/General/index
...
< HTTP/1.1 301 Moved Permanently
< Location: /HTTP/General

Using serve 9.2.0

Yes, please don't remove the slash in the end. It's breaking everything in the sub directory

If someone needs this now, http-server, as well as my own fork of node-static (which has been inactive and has vulnerabilities), @brettz9/node-static overcome this limitation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

malinda1986 picture malinda1986  路  6Comments

ajchambeaud picture ajchambeaud  路  4Comments

leo picture leo  路  5Comments

nikitamendelbaum picture nikitamendelbaum  路  6Comments

Mugen87 picture Mugen87  路  3Comments