staticFileServer.js

var http = require(‘http‘)
, fs = require(‘fs‘)
, url = require(‘url‘)
, path = require(‘path‘);

http.createServer(function(req, res) {
    var pathurl = url.parse(req.url).pathname;
    var abspath = __dirname + pathurl;
    fs.exists(abspath, function(exists) {
        if (exists) {
            //二进制方式读取文件
            fs.readFile(abspath, ‘binary‘, function(err, data) {
                if (err) {
                    res.writeHeader(500);
                    return res.end(‘index.html error‘);
                }
                var ext = path.extname(pathurl);
                var mini = ext ? ext.slice(1) : ‘unknow‘;
                res.writeHead(200, {‘Content-Type‘: MiniType[mini] || "text/plain", ‘The-key-is‘: ‘48435483121810xsafa2‘});
                res.write(data, ‘binary‘);
                res.end();
            })
        } else {
            res.end(‘404 File not found.‘);
        }
    })
}).listen(2233);

MiniType = {
    ‘323‘:‘text/h323‘,
    acx:‘application/internet-property-stream‘,
    ai:‘application/postscript‘,
    aif:‘audio/x-aiff‘,
    aifc:‘audio/x-aiff‘,
    aiff:‘audio/x-aiff‘,
    asf:‘video/x-ms-asf‘,
    asr:‘video/x-ms-asf‘,
    asx:‘video/x-ms-asf‘,
    au:‘audio/basic‘,
    avi:‘video/x-msvideo‘,
    axs:‘application/olescript‘,
    bas:‘text/plain‘,
    bcpio:‘application/x-bcpio‘,
    bin:‘application/octet-stream‘,
    bmp:‘image/bmp‘,
    c:‘text/plain‘,
    cat:‘application/vnd.ms-pkiseccat‘,
    cdf:‘application/x-cdf‘,
    cer:‘application/x-x509-ca-cert‘,
    ‘class‘:‘application/octet-stream‘,
    clp:‘application/x-msclip‘,
    cmx:‘image/x-cmx‘,
    cod:‘image/cis-cod‘,
    cpio:‘application/x-cpio‘,
    crd:‘application/x-mscardfile‘,
    crl:‘application/pkix-crl‘,
    crt:‘application/x-x509-ca-cert‘,
    csh:‘application/x-csh‘,
    css:‘text/css‘,
    dcr:‘application/x-director‘,
    der:‘application/x-x509-ca-cert‘,
    dir:‘application/x-director‘,
    dll:‘application/x-msdownload‘,
    dms:‘application/octet-stream‘,
    doc:‘application/msword‘,
    dot:‘application/msword‘,
    dvi:‘application/x-dvi‘,
    dxr:‘application/x-director‘,
    eps:‘application/postscript‘,
    etx:‘text/x-setext‘,
    evy:‘application/envoy‘,
    exe:‘application/octet-stream‘,
    fif:‘application/fractals‘,
    flr:‘x-world/x-vrml‘,
    gif:‘image/gif‘,
    gtar:‘application/x-gtar‘,
    gz:‘application/x-gzip‘,
    h:‘text/plain‘,
    hdf:‘application/x-hdf‘,
    hlp:‘application/winhlp‘,
    hqx:‘application/mac-binhex40‘,
    hta:‘application/hta‘,
    htc:‘text/x-component‘,
    htm:‘text/html‘,
    html:‘text/html‘,
    htt:‘text/webviewhtml‘,
    ico:‘image/x-icon‘,
    ief:‘image/ief‘,
    iii:‘application/x-iphone‘,
    ins:‘application/x-internet-signup‘,
    isp:‘application/x-internet-signup‘,
    jfif:‘image/pipeg‘,
    jpe:‘image/jpeg‘,
    jpeg:‘image/jpeg‘,
    jpg:‘image/jpeg‘,
    js:‘application/x-javascript‘,
    latex:‘application/x-latex‘,
    lha:‘application/octet-stream‘,
    lsf:‘video/x-la-asf‘,
    lsx:‘video/x-la-asf‘,
    lzh:‘application/octet-stream‘,
    m13:‘application/x-msmediaview‘,
    m14:‘application/x-msmediaview‘,
    m3u:‘audio/x-mpegurl‘,
    man:‘application/x-troff-man‘,
    mdb:‘application/x-msaccess‘,
    me:‘application/x-troff-me‘,
    mht:‘message/rfc822‘,
    mhtml:‘message/rfc822‘,
    mid:‘audio/mid‘,
    mny:‘application/x-msmoney‘,
    mov:‘video/quicktime‘,
    movie:‘video/x-sgi-movie‘,
    mp2:‘video/mpeg‘,
    mp3:‘audio/mpeg‘,
    mpa:‘video/mpeg‘,
    mpe:‘video/mpeg‘,
    mpeg:‘video/mpeg‘,
    mpg:‘video/mpeg‘,
    mpp:‘application/vnd.ms-project‘,
    mpv2:‘video/mpeg‘,
    ms:‘application/x-troff-ms‘,
    mvb:‘application/x-msmediaview‘,
    nws:‘message/rfc822‘,
    oda:‘application/oda‘,
    p10:‘application/pkcs10‘,
    p12:‘application/x-pkcs12‘,
    p7b:‘application/x-pkcs7-certificates‘,
    p7c:‘application/x-pkcs7-mime‘,
    p7m:‘application/x-pkcs7-mime‘,
    p7r:‘application/x-pkcs7-certreqresp‘,
    p7s:‘application/x-pkcs7-signature‘,
    pbm:‘image/x-portable-bitmap‘,
    pdf:‘application/pdf‘,
    pfx:‘application/x-pkcs12‘,
    pgm:‘image/x-portable-graymap‘,
    pko:‘application/ynd.ms-pkipko‘,
    pma:‘application/x-perfmon‘,
    pmc:‘application/x-perfmon‘,
    pml:‘application/x-perfmon‘,
    pmr:‘application/x-perfmon‘,
    pmw:‘application/x-perfmon‘,
    pnm:‘image/x-portable-anymap‘,
    pot:‘application/vnd.ms-powerpoint‘,
    ppm:‘image/x-portable-pixmap‘,
    pps:‘application/vnd.ms-powerpoint‘,
    ppt:‘application/vnd.ms-powerpoint‘,
    prf:‘application/pics-rules‘,
    ps:‘application/postscript‘,
    pub:‘application/x-mspublisher‘,
    qt:‘video/quicktime‘,
    ra:‘audio/x-pn-realaudio‘,
    ram:‘audio/x-pn-realaudio‘,
    ras:‘image/x-cmu-raster‘,
    rgb:‘image/x-rgb‘,
    rmi:‘audio/mid‘,
    roff:‘application/x-troff‘,
    rtf:‘application/rtf‘,
    rtx:‘text/richtext‘,
    scd:‘application/x-msschedule‘,
    sct:‘text/scriptlet‘,
    setpay:‘application/set-payment-initiation‘,
    setreg:‘application/set-registration-initiation‘,
    sh:‘application/x-sh‘,
    shar:‘application/x-shar‘,
    sit:‘application/x-stuffit‘,
    snd:‘audio/basic‘,
    spc:‘application/x-pkcs7-certificates‘,
    spl:‘application/futuresplash‘,
    src:‘application/x-wais-source‘,
    sst:‘application/vnd.ms-pkicertstore‘,
    stl:‘application/vnd.ms-pkistl‘,
    stm:‘text/html‘,
    svg:‘image/svg+xml‘,
    sv4cpio:‘application/x-sv4cpio‘,
    sv4crc:‘application/x-sv4crc‘,
    swf:‘application/x-shockwave-flash‘,
    t:‘application/x-troff‘,
    tar:‘application/x-tar‘,
    tcl:‘application/x-tcl‘,
    tex:‘application/x-tex‘,
    texi:‘application/x-texinfo‘,
    texinfo:‘application/x-texinfo‘,
    tgz:‘application/x-compressed‘,
    tif:‘image/tiff‘,
    tiff:‘image/tiff‘,
    tr:‘application/x-troff‘,
    trm:‘application/x-msterminal‘,
    tsv:‘text/tab-separated-values‘,
    txt:‘text/plain‘,
    uls:‘text/iuls‘,
    ustar:‘application/x-ustar‘,
    vcf:‘text/x-vcard‘,
    vrml:‘x-world/x-vrml‘,
    wav:‘audio/x-wav‘,
    wcm:‘application/vnd.ms-works‘,
    wdb:‘application/vnd.ms-works‘,
    wks:‘application/vnd.ms-works‘,
    wmf:‘application/x-msmetafile‘,
    wps:‘application/vnd.ms-works‘,
    wri:‘application/x-mswrite‘,
    wrl:‘x-world/x-vrml‘,
    wrz:‘x-world/x-vrml‘,
    xaf:‘x-world/x-vrml‘,
    xbm:‘image/x-xbitmap‘,
    xla:‘application/vnd.ms-excel‘,
    xlc:‘application/vnd.ms-excel‘,
    xlm:‘application/vnd.ms-excel‘,
    xls:‘application/vnd.ms-excel‘,
    xlt:‘application/vnd.ms-excel‘,
    xlw:‘application/vnd.ms-excel‘,
    xof:‘x-world/x-vrml‘,
    xpm:‘image/x-xpixmap‘,
    xwd:‘image/x-xwindowdump‘,
    z:‘application/x-compress‘,
    zip:‘application/zip‘
}

staticFileServer.js,布布扣,bubuko.com

时间: 2024-10-09 09:46:48

staticFileServer.js的相关文章

【API】高德地图API JS实现获取坐标和回显点标记

1.搜索+选择+获取经纬度和详细地址 2.回显数据并点标记 3.实现 第一步:引入资源文件 <!--引入高德地图JSAPI --><script src="//webapi.amap.com/maps?v=1.3&key=在官网申请一个key"></script><!--引入UI组件库(1.0版本) --><script src="//webapi.amap.com/ui/1.0/main.js">

js跨域

第一次写博客,好紧张,不知道能写成啥样,哈哈哈. 自己的一知片解,有错请多多指教,嘻嘻嘻. 一.何为跨域? 只要协议.域名.端口后任何一个不同,就是跨域. 举个例子: http://www.example.com 协议不同 https://www.example.com http://www.example.com 域名不同 http://www.test.com http://www.example.com 端口不同 http://www.example.com:81 注意:ip相同,域名不同

Vue.js学习笔记:属性绑定 v-bind

v-bind  主要用于属性绑定,Vue官方提供了一个简写方式 :bind,例如: <!-- 完整语法 --> <a v-bind:href="url"></a> <!-- 缩写 --> <a :href="url"></a> 绑定HTML Class 一.对象语法: 我们可以给v-bind:class 一个对象,以动态地切换class.注意:v-bind:class指令可以与普通的class特

node.js的安装及配置

一.安装 直接在浏览器搜索node.js,在官网上下载(一般旧版的更加稳定,比如下载4.4.7版本) 点击DOWNLOADS 往下翻,点击Previous Release Windows下载msi(64位/32位) 根据提示一步步安装,安装之后的文件夹如下: 在cmd命令行下输入node -v,如果出现如下,说明安装成功: 二.关于配置 在安装路径下新建两个文件夹: 创建完两个空文件夹之后,打开cmd命令窗口,输入 npm config set prefix "D:\Program Files

Node.js 使用angularjs取得Nodejs http服务端返回的JSON数组示例

server.js代码: // 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http"); // 创建服务器,创建HTTP服务器要调用http.createServer()函数,它只有一个参数,是个回调函数,服务器每次收到http请求后都会调用这个回调函数.服务器每收到一条http请求,都会用新的request和response对象触发请求函数. var server=http.createS

Knockout.js简介

Knockout是一款很优秀的JavaScript库,通过应用MVVM模式使JavaScript前端UI简单化.任何时候你的局部UI内容需要自动更新,KO都可以很简单的帮你实现,并且非常易于维护. Knockout的3个核心功能是: ? 属性监控与依赖跟踪 ? 声明式绑定 ? 模板机制 MVVM Model-View-View Model (MVVM)是一种创建用户界面的设计模式. ? Model:用于存储应用程序数据,表示业务领域的对象和数据操作,并且独立于任何界面. 当使用KO的时候,通常是

vue.js 入门

简单一句话来描述:vue.js是一个前端框架. 官方文档:https://cn.vuejs.org/v2/guide/index.html 虽然,我以前也会改一些前端样式,但主要是基于HTML和CSS,HTML主要控制页面的结构,CSS主要来控制样式.涉及到JavaScript就比较小白了. 我花了一个下午照着官方文档做练习,当然是只创建一个xxx_demo.html文件,在<script></script> 标签对之间写 Vue.js语法.这不算错,但在工程化的今天,这么学得猴年

JS高程3:JSON

JSON,JavaScript Object Notation,JS对象表示法,是目前最常见的结构化数据传输形式. JSON并非编程语言,而是一种数据结构,像mp4.avi一样,只是一种数据格式而已.(数据结构可以包含很多数据类型) JSON值的类型 简单值 对象 数组 简单值:字符串.数字.布尔值和null,注意不包括undefined. 注意:JSON中的字符串必须用双引号. 对象:对象就是无序的键值对,而键值中的值也可以是简单值.对象或者数组. 注意:JSON中对象的属性必须用双引号括起来

js装饰器

本文是廖雪峰老师js教程的学习笔记 JavaScript的所有对象都是动态的,即使内置的函数,我们也可以重新指向新的函数. 利用apply(),我们还可以动态改变函数的行为. 现在假定我们想统计一下代码一共调用了多少次parseInt(),可以把所有的调用都找出来,然后手动加上count += 1,不过这样做太傻了.最佳方案是用我们自己的函数替换掉默认的parseInt(): var count = 0; var oldParseInt = parseInt; // 保存原函数 window.p