nodejs使用nodejs創建簡單的靜態文件服務器

在開始之前,應該好好規劃一下項目的文件目錄了。我的目錄結構如下:

assets放置網站的靜態文件css,js,img等;common存放項目的配置文件和一些通用文件;server存放服務處理文件,將要創建的靜態文件服務就是放在此目錄中; tpl放置的是模板文件也就是網頁文件。
文件的下載格式主要是由'Content-Type'的值決定的,要想下載的文件能夠正常工作就應該正確的設置不同文件的'Content-Type'值。mime.js文件存放了一些常用mime值:

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的類容:

//  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[type]});  
                        res.write(file, 'binary');  
                        res.end();  
                    }  
                });  
            }  
        })  
    }  
    exports.filesLoad = filesLoad;  
上面引入了nodejs的內置模塊http、fs、url、path,config和FServer是自定義模塊,要讀取文件首先要知道文件在磁盤上是否存在,還應當知道文件的類型才能達到想要的效果。運行程序可以發現css和javascript都下載正確,並且css效果在頁面上正確渲染(javascript還沒有寫效果)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章