jfhbrook / node-ecstatic

A static file server middleware that works with core http, express or on the CLI!
https://github.com/jfhbrook/node-ecstatic
MIT License
975 stars 194 forks source link

consider using relative url for href to work behind a proxy #233

Closed panlina closed 5 years ago

panlina commented 6 years ago

Hi, I'm using ecstatic behind a proxy, something like proxying http://localhost/a to http://localhost:8080, where 8080 is the port ecstatic is listening to. The problem is that ecstatic uses absolute url for href, which resolves to a url where the file path is directly appended to the host part, instead of the current url. For instance, when browsing from http://localhost/a, clicking folder f navigates to http://localhost/f/, instead of http://localhost/a/f/. I noticed that there's a baseDir option, but it's irrelevant here since the base url(/a) is already eaten by the proxy before baseDir applies. Using relative url for href may be a solution, but I'm not sure whether you are using absolute path for some reason. How do you think?

wll8 commented 5 years ago

@panlina Hello, how do you deal with this problem?

panlina commented 5 years ago

It can not be solved unless you modify the code. You may ask the author to take this into account.

jfhbrook commented 5 years ago

panlina made a pull request that tries to address this but removes functionality that I'm not immediately prepared to ditch: https://github.com/jfhbrook/node-ecstatic/pull/237

If you can propose a concrete change that does what you need without removing the baseDir option, I'm all for it.

wll8 commented 5 years ago

show-dir/index.js#L98: You can switch these two lines of code by option.

// let href = `${parsed.pathname.replace(/\/$/, '')}/${encodeURIComponent(file[0])}`;
let href = `${encodeURIComponent(file[0])}`;

The problem between absolute path and relative path can be solved.

jfhbrook commented 5 years ago

Oh! If that's all that's needed I'll gladly change that behavior, I just need the time to manually QA it

panlina commented 5 years ago

Yes, that's all that is needed, but I think an option is not needed, just change it to be relative path.

wll8 commented 5 years ago

Options are used to ensure that they do not conflict with the author's previous ideas.

panlina commented 5 years ago

Understood. That's what I'm asking at the end of my post.