Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add MIME type with Express

I'm trying to get Firefox to play a video tag. Normally, I would just add this to an .htaccess file on Apache:

AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm

AddType audio/mpeg .mp3
AddType audio/ogg .ogg
AddType audio/mp4 .m4a
AddType audio/wav /wav

How would I do this with Express / NodeJS?

like image 807
Allen Avatar asked Mar 03 '14 21:03

Allen


2 Answers

For express 4.x, good documentation on mime-type can be found in https://github.com/broofa/node-mime.

For example, Safari browser would show the content of csv instead of downloading the csv with <a href="some.csv">download here</a>.

You can get around this by adding the following

express.static.mime.define({'application/octet-stream': ['csv']})
like image 96
heatherz Avatar answered Sep 27 '22 17:09

heatherz


Assuming it's a video in a public directory, you can use the static middleware for this:

app.use(express.static(__dirname + '/public'));

If you need to alter the mime table (like to add or change an extension, do):

express.mime.type['ogv'] = 'video/ogg';

But I think all the ones you listed are already there.

Then requests to /foo.wav will serve up /public/foo.wav with the proper content-type header, provided no other middleware handles the route first.

like image 28
sgress454 Avatar answered Sep 27 '22 16:09

sgress454