How to add MIME type with Express

后端 未结 3 1249
别跟我提以往
别跟我提以往 2021-01-18 10:52

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         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 11:05

    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.

提交回复
热议问题