Comments (9)
The problem with links is mostly benign, I think. I haven't seen a relative link in the wild in years. Either way, if you're setting an option to remove trailing slashes you should understand the consequences.
You'd be surprised :) I am constantly helping new web programmers with little to no understanding of any of this, and would expect it to look the same way as when they double-click on the file on their file system.
I don't think having the option you propose would be valid for this project now that I better understand it, I'm sorry. You're welcome to use a different project or similar if that is really what you want to do without having a file system layout that matches the files served. But in reality, files should never be served up by Node.js unless as a last resort, so you should be using Apache/NGINX/etc. I'm not sure if they have those options you're looking for, either, but if they do, that would probably be the best overall solution.
from serve-static.
Hi @fabiosantoscode . I wonder what the problem is, first. Can you provide a reproduction case that we can debug through to see the issue you are having? Just modify one of the examples in https://github.com/expressjs/serve-static#examples and provide the modified code here and the steps to see what is happening and then what you expect to happen instead. Thanks!
from serve-static.
@dougwilson certainly!
I've just noticed, by trying to reproduce this myself, that this statement was incorrect:
even if it's possible to stop from redirecting to a trailing slash
However, the main issue still holds.
I'm going to paste my code and describe how I built the "public" directory, but let me know if you'd like me to create a gist or a github repository.
var express = require('express')
var serveStatic = require('serve-static')
var app = express()
app.use(serveStatic('public', { redirect: false }))
app.listen(4444)
This is what's in the public folder (the index.html file is just an empty file, since it doesn't matter what it contains)
public/
└── foo
└── index.html
To reproduce the issue I'm describing, I've used curl:
(venv) fabio@fabio-thinkpad ♥ curl -I localhost:4444/foo
HTTP/1.1 404 Not Found
[...other headers]
(curl -I localhost:4444/foo/
yields a 200 OK response)
I would like a way to serve /foo
here by reading whatever is in its index.html
file. And also for /foo/
to be redirected to just /foo
. This might be two options, or one option. Either way I'm happy to send a PR.
Thanks for the speedy reply!
from serve-static.
Ah, I see. I don't think that's going to work, though, as all the relative links on the index.html
would not work in that case, right?
from serve-static.
Any reason why not to just do public/foo.html
to achieve the same thing? Then the file would be logically in the proper relative directly to which it would be served out it. The https://github.com/expressjs/serve-static#extensions option would make that work for the url /foo
from serve-static.
public/foo.html
would certainly achieve the same thing. I didn't think of that at all. I'm not sure if that's a good option for the Gatsby project, but I'll check.
The problem with links is mostly benign, I think. I haven't seen a relative link in the wild in years. Either way, if you're setting an option to remove trailing slashes you should understand the consequences.
from serve-static.
Thank you!
from serve-static.
Hi @dougwilson, random question for you... I've recently started working on a Nginx/Nodejs stack and read part of your comment above:
...files should never be served up by Node.js unless as a last resort...
I've suspected this to be true for a while but don't yet fully understand the rationale. Do you mind explaining the reasoning behind this?
from serve-static.
Hi @formulahunter this is a general Node.js question I would suggest asking on Node.js itself to get the full answer. The short answer is that serving files will consume your worker pool (https://nodejs.org/en/docs/guides/dont-block-the-event-loop/).
from serve-static.
Related Issues (20)
- Need support node >= v10.x HOT 1
- how to prevent a file from being served HOT 2
- 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.