Comments (19)
I think this should be opt-in, since this middleware is supposed to be boilerplate basic serving files and not redirecting.
Yes, it should be opt-in, I agree. I'll make sure to swap the default value on the next major version bump.
That or make the detect for redirect better since it is being overzealous and breaking my url.
When the redirect occurs, what does Node.js see as the req.url
? If Apache is removing the /mypath
from the URL before giving the request to Node.js, I don't see how anything in Node.js could redirect to the proper URL.
from serve-static.
node sees req.url as /
since this is a sub-app. the thing is, when not nested under apache, the same is true. original url is /subapp
. the same is true when not nested under apache. So strange, really.
from serve-static.
as far as redirecting, i use config.baseURL to redirect properly when nested under apache. so potentially you could redirect relatively.. since yea, you couldn't know my config values.
from serve-static.
Yea, how about req.originalUrl
? That is the true URL that node.js saw.
from serve-static.
as far as redirecting, i use config.baseURL to redirect properly when nested under apache. so potentially you could redirect relatively.. since yea, you couldn't know my config values.
What you could do is use a module like on-headers
and dynamically alter all outgoing Location
headers to fix the path based on your config.baseURL
, then redirects would work magically.
from serve-static.
hmmmm.. now that i went through my whole app adding the prefix.. i'll look into it. is that a proper standard or is it still a good idea for you to redirect relatively?
from serve-static.
is that a proper standard or is it still a good idea for you to redirect relatively?
sorry, I don't understand the question :) technically the Location
header must be an absolute URL, but most browsers will allow relative URLs, if that's what you're asking.
from serve-static.
hmm so on-headers
fires last, right before the request goes out, and i should add /mypath
base route then? if a redirect happened? Seems okay, since my current adding of config.baseURL
to every redirect is wet. I might use it to clean up my code, however I don't think you should require people to use it if they use serve-static
nested under apache, but you already said you agree about opt-in.
I'm kind of curious about the redirect code anyway, does it just add slash? And just for the purpose of cleaning up the url? because if so I think you could get away with removing it unless it has a bigger use.
Sounds like relative redirects aren't supported as a standard, so that wouldn't work.
If you really need the redirect part, could I suggest opt-in by letting the user input either redirect: true
, or redirect: '/mypath'
? That way if someone wanted to use redirects and apache, they could with relative ease
from serve-static.
hmm so on-headers fires last, right before the request goes out, and i should add /mypath base route then? if a redirect happened?
Simple example (added very high in your middleware stack):
var onHeaders = require('on-headers')
app.use(function (req, res, next) {
onHeaders(res, function () {
var location = this.getHeader('Location')
if (location) {
this.setHeader('Location', config.baseURL + location)
}
})
next()
})
but you already said you agree about opt-in.
Yea, I don't think should do that by default.
I'm kind of curious about the redirect code anyway, does it just add slash? And just for the purpose of cleaning up the url? because if so I think you could get away with removing it unless it has a bigger use.
https://github.com/expressjs/serve-static/blob/master/index.js#L91 it adds a /
to the end if the referenced URL is a directory on the file system. This was so it would be used in conjunction with serve-index
.
If you really need the redirect part, could I suggest opt-in by letting the user input either redirect: true, or redirect: '/prefix' ?
Yea, I can probably change the send
module to accept a string value that would be treated as a prefix.
from serve-static.
It doesn't seem to include serve-index
though. So, why is it better to massage the path in this module rather than in serve-index
?
This is what I was wondering when I originally saw the piece of code since I didn't think there was much directory support.
is the index: ['index.html']
pointing to an existing file or will it serve index.html
as a list of directory contents? I can't seem to get that to do anything.
from serve-static.
is the index: ['index.html'] pointing to an existing file or will it serve index.html as a list of directory contents? I can't seem to get that to do anything.
If you put a index.html
file in a directly, it'll serve that file if you just ask for the directly, just like Apache would do.
from serve-static.
For example, put a file index.html
in __dirname, 'public'
and then load /limby_static
and it'll display the index.html
file, but the reason the redirect occurs is because that URL must be /limby_static/
(with the trailing slash), otherwise you cannot link to things reliably on the page.
from serve-static.
In case it helps, you were talking about Apache, so giving you some examples may help.
The index
option here is just like http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryindex (notice it is index.html
by default, just like with this module)
The redirect
options here is just like http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash (notice it is On
by default, just like with this module)
from serve-static.
I see what you mean now. Serving index.html when they hit the parent directory just like an apache www will do. I was thinking hitting the parent directory would give you a list of the files in that directory, as if you were on a read only ftp client.
I guess I didn't really expect people to use node to serve straight html like that but I guess they could.
I still don't see the need to redirect, even if you're serving indexes like this though, right?
from serve-static.
I still don't see the need to redirect, even if you're serving indexes like this though, right?
Say your index.html
page as the following HTML:
<img src="ball.gif">
to show a bouncing ball. The ball.gif
file would be located in the same folder as index.html
. I'm sure you can agree this makes sense. Now, say that folder is example
. If you go to http://localhost/example/
, the index.html
file will load and the browser will load http://localhost/example/ball.gif
and you'll see a bouncing ball!
Now, if you don't redirect, when a user goes to http://localhost/example
, then the browser will resolve the image to http://localhost/ball.gif
and it'll be a broken image.
from serve-static.
ah.. hmm. It's unlikely that anyone will find the solution would be {redirect: true}
in serve-static
if they had that issue and it was opt-in. They'd most likely change their links. I could see leaving it default or changing it, since not many people use html anyway. Do more people nest under apache or use flat html files?
from serve-static.
Do more people nest under apache or use flat html files?
hm, I'm not sure what the question really means, but this is a common problem with using index.html
pages, which is very common.
from serve-static.
well it might be better to leave it in as default then, since nesting under an apache route is probably more rare.
from serve-static.
So yea. As long as this module is doing index.html
serving, I'm going to keep it with the redirect stuff.
from serve-static.
Related Issues (20)
- Need support node >= v10.x HOT 1
- how to prevent a file from being served HOT 2
- Add option to remove trailing slashes, as opposed to adding them HOT 9
- Support gzip static serving HOT 8
- Please add types for typescript HOT 1
- FR: Ability to always call next() HOT 8
- Extensions vs Directory HOT 1
- feature request: enable mapping url to a different file via a callback HOT 1
- Question: Is it possible to access the origin from setHeaders()? HOT 4
- Issue with Example Code HOT 4
- Make 'immutable' cache-control value HTTP/3 QPACK compatible HOT 1
- Use seconds instead of milliseconds for maxAge HOT 2
- 404 returned insted of 403 when uisng the dotfiles with "deny option" HOT 8
- setHeaders: need to access req.query HOT 2
- Corrupted file served if file is changed during serving. HOT 4
- Omitted `Content-Type` header when passing index file HOT 2
- Angular 13 SSR serve is failing with error - TypeError: Right-hand side of 'instanceof' is not an object HOT 5
- How to disable range requests in Next.js ? HOT 1
- Cannot use namespace 'HttpError' as a type HOT 2
- When the index is set to false, requests ending with / are being served with the content type header set to application/octet-stream instead of actual file's content type HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serve-static.