Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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 #160

Closed
rmhaiderali opened this issue Jan 17, 2024 · 5 comments

Comments

@rmhaiderali
Copy link

For example, when requesting /index.html/, the server is responding with a Content-Type header set to application/octet-stream instead of text/html.

index.js

import express from "express";
const app = express();

app.use(express.static("public", { index: false }));

app.get("/", (req, res) => res.send("Hello World"));

app.listen(3000, () => console.log("server started: http://localhost:3000"));

public/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    HTML File
  </body>
</html>
@dougwilson
Copy link
Contributor

Hello, and thank you for the report, I will investigate. The index.html file should not be sent at all in your example, so we'll want to fix that 👍

@rmhaiderali
Copy link
Author

rmhaiderali commented Jan 17, 2024

I thought maybe they implemented it this way intentionally. However, it would be more logical not to serve the file at all.

@rmhaiderali
Copy link
Author

I'm not sure where the error is coming from, but it happens when server is running in a windows environment.

@rmhaiderali
Copy link
Author

Hello @dougwilson,

Windows intentionally implemented this behavior where, when accessing a path, if it ends with "/", instead of treating it as a directory, it checks if a file with the same name exists. In such cases, it ignores the "/" and allows users to access files as if the path did not end with "/".

I'm unsure about which package (serve-static or send) we should modify to include a check that restricts serving a file if its path ends with "/".

@wesleytodd
Copy link
Member

I believe this is resolved here: pillarjs/send#224

I will likely try to land that as the first minor release on send once we get express@5 out the door. I am going to close this and track this update in send since that is where the fix should be. @rmhaiderali if you would like to validate the fix there I would greatly appreciate it.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants