zoukankan      html  css  js  c++  java
  • nodejsmime类型

    MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型。是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。

    简单点说,mime是一个互联网标准,通过设定它就可以设定文件在浏览器的打开方式。




    1、对于css文件,可以直接提取后缀名,如
    if(suffix==".css"){
          res.writeHead("Content-type":"text/css")
    }else if(suffix==".html"){
          res.writeHead("Content-type":"text/html")
    }
    //为了直观,原谅我用了if-else
    

    这种方法其实就是mime模块实现的基础。如前所述,mime是设定文件类型的,csshtml文件的类型归类是"text/",但是视频文件3gp等是"video/",图片文件jpeg是"image/",······ 互联网上还有很多很多的格式,而我们又必须它们一个一个都实现,这才有了统一又方便可重用的mime模块

    文末也会贴出近乎所有mime类型

    2、使用mime模块设定文件类型
    //cnpm install mime  下载mime模块
    var mime=require("mime")//引入mime模块
     res.writeHead(200,{'Content-type':mime.getType(filePath)});    //通过后缀名指定mime类型
     res.end(data);
    
    mime使用方法:
    • 使用mime模块查询文件的MIME类型:
    mime.getType('/path/to/file.txt');         // => 'text/plain'
    mime.getType('file.txt');                  // => 'text/plain'
    mime.getType('.TXT');                      // => 'text/plain'
    mime.getType('htm');                       // => 'text/html'
    
    • 查询文件护展名mime.getExtension(type)
    mime.getExtension('text/html');                 // => 'html'
    mime.getExtension('application/octet-stream');  // => 'bin'
    
    • mime自定义类型(当mime模块自带的mime-db库不存在或不能满足我们所需的MIME类型时,还可以自定义MIME类型)
    mime.define({
        'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'],
        'application/x-my-type': ['x-mt', 'x-mtt'],
        // etc ...
    });
    
    mime.lookup('x-sft');                 // => 'text/x-some-format'
    

    veblen's promise

    exports.types = {
    'hqx':'application/mac-binhex40',
    'cpt':'application/mac-compactpro',
    'csv':['text/x-comma-separated-values', 'text/comma-separated-values',   'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'],
    'bin':'application/macbinary',
    'dms':'application/octet-stream',
    'lha':'application/octet-stream',
    'lzh':'application/octet-stream',
    'exe':['application/octet-stream', 'application/x-msdownload'],
    'class':'application/octet-stream',
    'psd':'application/x-photoshop',
    'so':'application/octet-stream',
    'sea':'application/octet-stream',
    'dll':'application/octet-stream',
    'oda':'application/oda',
    'pdf':['application/pdf', 'application/x-download'],
    'ai':'application/postscript',
    'eps':'application/postscript',
    'ps':'application/postscript',
    'smi':'application/smil',
    'smil':'application/smil',
    'mif':'application/vnd.mif',
    'xls':['application/excel', 'application/vnd.ms-excel', 'application/msexcel'],
    'ppt':['application/powerpoint', 'application/vnd.ms-powerpoint'],
    'wbxml':'application/wbxml',
    'wmlc':'application/wmlc',
    'dcr':'application/x-director',
    'dir':'application/x-director',
    'dxr':'application/x-director',
    'dvi':'application/x-dvi',
    'gtar':'application/x-gtar',
    'gz':'application/x-gzip',
    'php':'application/x-httpd-php',
    'php4':'application/x-httpd-php',
    'php3':'application/x-httpd-php',
    'phtml':'application/x-httpd-php',
    'phps':'application/x-httpd-php-source',
    'js':'application/x-javascript',
    'swf':'application/x-shockwave-flash',
    'sit':'application/x-stuffit',
    'tar':'application/x-tar',
    'tgz':['application/x-tar', 'application/x-gzip-compressed'],
    'xhtml':'application/xhtml+xml',
    'xht':'application/xhtml+xml',
    'zip':['application/x-zip', 'application/zip', 'application/x-zip-compressed'],
    'mid':'audio/midi',
    'midi':'audio/midi',
    'mpga':'audio/mpeg',
    'mp2':'audio/mpeg',
    'mp3':['audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'],
    'aif':'audio/x-aiff',
    'aiff':'audio/x-aiff',
    'aifc':'audio/x-aiff',
    'ram':'audio/x-pn-realaudio',
    'rm':'audio/x-pn-realaudio',
    'rpm':'audio/x-pn-realaudio-plugin',
    'ra':'audio/x-realaudio',
    'rv':'video/vnd.rn-realvideo',
    'wav':['audio/x-wav', 'audio/wave', 'audio/wav'],
    'bmp':['image/bmp', 'image/x-windows-bmp'],
    'gif':'image/gif',
    'jpeg':['image/jpeg', 'image/pjpeg'],
    'jpg':['image/jpeg', 'image/pjpeg'],
    'jpe':['image/jpeg', 'image/pjpeg'],
    'png':['image/png', 'image/x-png'],
    'tiff':'image/tiff',
    'tif':'image/tiff',
    'css':'text/css',
    'html':'text/html',
    'htm':'text/html',
    'shtml':'text/html',
    'txt':'text/plain',
    'text':'text/plain',
    'log':['text/plain', 'text/x-log'],
    'rtx':'text/richtext',
    'rtf':'text/rtf',
    'xml':'text/xml',
    'xsl':'text/xml',
    'mpeg':'video/mpeg',
    'mpg':'video/mpeg',
    'mpe':'video/mpeg',
    'qt':'video/quicktime',
    'mov':'video/quicktime',
    'avi':'video/x-msvideo',
    'movie':'video/x-sgi-movie',
    'doc':'application/msword',
    'docx':['application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'],
    'xlsx':['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'],
    'word':['application/msword', 'application/octet-stream'],
    'xl':'application/excel',
    'eml':'message/rfc822',
    'json':['application/json', 'text/json']
    };
    

    Do it well, I've been here anytime!



    作者:Veb
    链接:https://www.jianshu.com/p/1ce720f20fc1
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    Google开源单元測试框架Google Test:VS2012 配置
    ubuntu16.04 uninstall cuda 9.0 completely and install 8.0 instead
    ubuntu 16.04 安装cuda的方法
    ubuntu垃圾文件清理方法
    行人检测资源(下)代码数据
    行人检测资源(上)综述文献
    开源深度学习架构Caffe
    python pip 安装库文件报错:pip install ImportError: No module named _internal
    Canny算子
    vmware中nat模式中使用静态ip后无法上网的问题
  • 原文地址:https://www.cnblogs.com/weifeng1463/p/15522698.html
Copyright © 2011-2022 走看看