expressjs / serve-static

Serve static files
MIT License
1.38k stars 227 forks source link

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 closed 3 weeks ago

rmhaiderali commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

rmhaiderali commented 7 months ago

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

rmhaiderali commented 7 months ago

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 commented 3 weeks ago

I believe this is resolved here: https://github.com/pillarjs/send/pull/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.