kaiser_cn 发表于 2017-2-21 11:32:36

nodejs使用nodejs创建简单的静态文件服务器

  转自:http://blog.csdn.net/zxsrendong/article/details/16804867
在开始之前,应该好好规划一下项目的文件目录了。我的目录结构如下:

assets放置网站的静态文件css,js,img等;common存放项目的配置文件和一些通用文件;server存放服务处理文件,将要创建的静态文件服务就是放在此目录中; tpl放置的是模板文件也就是网页文件。
文件的下载格式主要是由'Content-Type'的值决定的,要想下载的文件能够正常工作就应该正确的设置不同文件的'Content-Type'值。mime.js文件存放了一些常用mime值:
 view plaincopy 



[*]exports.mime = {  
[*]    "html" : "text/html",  
[*]    "css"  : "text/css",  
[*]    "js"   : "text/javascript",  
[*]    "json" : "application/json",  
[*]    "ico"  : "image/x-icon",  
[*]    "gif"  : "image/gif",  
[*]    "jpeg" : "image/jpeg",  
[*]    "jpg"  : "image/jpeg",  
[*]    "png"  : "image/png",  
[*]    "pdf"  : "application/pdf",  
[*]    "svg"  : "image/svg+xml",  
[*]    "swf"  : "application/x-shockwave-flash",  
[*]    "tiff" : "image/tiff",  
[*]    "txt"  : "text/plain",  
[*]    "wav"  : "audio/x-wav",  
[*]    "wma"  : "audio/x-ms-wma",  
[*]    "wmv"  : "video/x-ms-wmv",  
[*]    "xml"  : "text/xml"  
[*]};  

  先来看server.js和FServer.js的类容:
 
 
 view plaincopy 



[*]//  server.js    
[*]    var config = require('./common/config');    
[*]    var http   = require('http');    
[*]    var fs     = require('fs');    
[*]    var url    = require('url');    
[*]    var path   = require('path');    
[*]    var FServer   = require('./server/FServer');    
[*]    function index(){    
[*]        var indexPath = config.ui + '/index.html';    
[*]        fs.exists(indexPath, function(exists){    
[*]            if( !exists ) {    
[*]                throw err;    
[*]            } else {    
[*]                fs.readFile(indexPath, function(err, data){    
[*]                    if (err) {    
[*]                        throw err;      
[*]                    } else {    
[*]                        function onRequest(req, res){    
[*]                            // 取得文件路径    
[*]                            var pathname = url.parse(req.url).pathname;    
[*]                            // 获取文件扩展名(包含前置.)    
[*]                            var extname = path.extname( pathname );    
[*]                            var type = extname.slice(1);    
[*]                            // 获取下载文件在磁盘上的路径,    
[*]                            var realPath = config.root + pathname;    
[*]                            if ( extname === '' ) {    
[*]                                res.writeHead(200, {'Content-Type':'text/html'});    
[*]                                res.write(data);    
[*]                                res.end();    
[*]                            } else {    
[*]                                FServer.filesLoad(realPath, type, req, res);    
[*]                            }    
[*]                        }    
[*]                        http.createServer(onRequest).listen(config.port);    
[*]                    }    
[*]                })    
[*]            }    
[*]        })    
[*]     }    
[*]    exports.index = index;    
[*]    
[*]    // FServer.js    
[*]    var fs   = require('fs');    
[*]    var mime = require('../common/mime').mime;    
[*]    function filesLoad(filePath, type, req, res){    
[*]        fs.exists(filePath, function(exists){    
[*]            if ( !exists ) {    
[*]                res.writeHead(404, {'Content-Type': 'text/plain'});    
[*]                // res.write();    
[*]                res.end();    
[*]            } else {    
[*]                fs.readFile(filePath, 'binary', function(err, file){    
[*]                    if ( err ) {    
[*]                        res.writeHead(500, {'Content-Type': 'text/plain'});    
[*]                        // res.write();    
[*]                        res.end();    
[*]                    } else {    
[*]                        res.writeHead(200, {'Content-Type': mime});    
[*]                        res.write(file, 'binary');    
[*]                        res.end();    
[*]                    }    
[*]                });    
[*]            }    
[*]        })    
[*]    }    
[*]    exports.filesLoad = filesLoad;    

  上面引入了nodejs的内置模块http、fs、url、path,config和FServer是自定义模块,要读取文件首先要知道文件在磁盘上是否存在,还应当知道文件的类型才能达到想要的效果。运行程序可以发现css和javascript都下载正确,并且css效果在页面上正确渲染(javascript还没有写效果)
   
 
页: [1]
查看完整版本: nodejs使用nodejs创建简单的静态文件服务器