node.js搭建简单静态文件服务器
node.js搭建简单静态文件服务器
761218914 发表于2年前
node.js搭建简单静态文件服务器
  • 发表于 2年前
  • 阅读 14
  • 收藏 0
  • 点赞 2
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: 是从这里看到的http://page.renren.com/601228958/note/884506744

是从这里看到的http://page.renren.com/601228958/note/884506744完美可用

//server.js

var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var types = require('./mimes').types;

http.createServer(function(req,res){

//文件在本地的路径

    var b = 'C:/data/';
   // var p = 'assets' + url.parse(req.url).pathname;
    var p = url.parse(req.url).pathname;
    var realpath = b + p;

    fs.exists(realpath,function(exists){
        if(!exists){
            res.writeHead(404,{
                'Content-type':'text/html;charset=utf-8'
            });

            res.write('<h1>404 NOT FOUND</h1>');
            res.write(p + ' doesn\'t exists.');
            res.end();
        }else{
            fs.readFile(realpath,'binary',function(err,data){
                if(err){
                    res.writeHead(500,{
                        'Content-type':'text/html;charset=utf-8'
                    });
                    res.end(err);
                }else{

                    var ext = path.extname(realpath);

                    ext = ext ? ext.slice(1) : 'unknown';

                    var contentType = types[ext] || 'text/plain';

                    res.writeHead(200,{
                        'Content-type':contentType
                    });

                    res.write(data,'binary');
                    res.end();
                }
            });
        }
    });

}).listen(80);

console.log('Server running at localhost:80'); 


//mimes.js
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']
};

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 5
博文 103
码字总数 47263
×
761218914
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: