expressjs / serve-static

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

Omitted `Content-Type` header when passing index file #156

Closed giovanni-bertoncelli closed 1 year ago

giovanni-bertoncelli commented 1 year ago

Hi, I'm using this middleware in order to serve static files. My configuration is something like this:

    serveStatic('./public', {
      index: isDev ? 'index.html' : 'index.prod.html'
    })

But when navigating on the root, since I have the X-Content-Type-Options: nosniff header enabled, I've noticied that the index.html file is returned without the Content-type header, so the HTML get not rendered by the browser:

image

I think that header should be present, shouldn't it?

dougwilson commented 1 year ago

Hello, and sorry you are having an issue. It should be there. I tried really quick to put together an example app using the code snippet you provided and I was unable to reproduce your issue. Can you either modify the below code and then detail how to call it where it does not return a content-type header, or provide your code fully-runnable code and instructions on how to reproduce? We would be happy to diagnose and address the issue, but just are unable to reproduce with the given information.

var finalhandler = require('finalhandler')
var http = require('http')
var serveStatic = require('serve-static')

var serve = serveStatic('./public', { index: 'index.html' })

var server = http.createServer(function onRequest (req, res) {
  serve(req, res, finalhandler(req, res))
})

server.listen(3000)
$ ls public/
index.html
$ curl -i http://localhost:3000/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Wed, 15 Feb 2023 17:01:10 GMT
ETag: W/"0-1865605dec6"
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Date: Wed, 15 Feb 2023 17:02:26 GMT
Connection: keep-alive
Keep-Alive: timeout=5
giovanni-bertoncelli commented 1 year ago

Thank you for the quick response @dougwilson , I think I've mistaken with some other static content served by my server.