I was building a server with express that uses
app.use(express.static("public"));
to serve any static file requests that come by. The problem is that the code doesn't automatically set the Content-Type header (or any header it seems, correct me if I'm wrong). In the documentation for express.static()
, they say you could send in an options
object as a second parameter to express.static()
and specify a function that would set any headers, like
app.use(express.static("public", {
setHeader : function (res, path, stat) {
//some code to set headers
}
}));
The documentation I read is here.
The problem with this that I found is that you'd have to write code to set the header individually for each file type. And even if I decided to do that, I found no way to access the filename of the file that is being served. Is there a standard way of doing this?