Search code examples
expressmime-typesresponse-headers

Express sendFile with custom mimetype


I have an image upload system where files are stored with a uuid as a name. Theres also a database entry that stores the orignal mimetype of a file.
I would like to use express sendFile function to serve the file to the user, but unfortunately, it will set the mimetype based on the file extension, which usually looks like this:

uploads/b56adbe4-6ab6-47f1-8db3-41eeb43e765c

because it doesn't have a file extension the mimetype is always set to application/octet-stream. So is there a way to change the mimetype to the correct one stored in the database? So that the browser can display the image?


Solution

  • I found the solution, just set the content type header:

                res.sendFile(body.file, { 
                    root : "./",
                    headers:{
                        "Content-Type":body.mimeType
                    }
                }