Express: Setting content-type based on path/file?

后端 未结 6 1983
闹比i
闹比i 2020-12-09 16:46

I know Express has the res.contentType() method, but how to set automatically content type based on path/file (including static content)?

6条回答
  •  忘掉有多难
    2020-12-09 17:13

    Run the following cmd :

    npm install xmimetype ; 
    

    Then , in your code :

      var xm=require("xmimetype");
    
      xm.mimetypeOf("java"); 
      xm.mimetypeOf("./lib/Person.java"); 
      // -> text/x-java-source
    
      xm.mimetypeOf("docx"); 
      xm.mimetypeOf("./lib/overview.docx"); 
      // -> application/vnd.openxmlformats-officedocument.wordprocessingml.document
    

    For more info , check GIT repository .


    The opposite is available :

      xm.extensionsOf("image/jpeg");
     // -> [ 'jpeg', 'jpg', 'jpe' ]
    

提交回复
热议问题