1. 程式人生 > >Node.js WEB伺服器靜態檔案託管

Node.js WEB伺服器靜態檔案託管

封裝靜態WEB伺服器

原本我們的主程式中的程式碼如下:

//引入http模組
var http=require('http');

//fs模組

var fs = require('fs');

//path模組
var path = require('path');  /*nodejs自帶的模組*/

//url模組

var url = require('url');

//引入副檔名的方法是在檔案裡面獲取到的。

var mimeModel = require('./model/getmimefromfile.js');

//console.log(mimeModel.getMime('.css'));   //獲取檔案型別
http.createServer(function(req,res){ //http://localhost:8001/news.html /news.html //http://localhost:8001/index.html /index.html //css/dmb.bottom.css //xxx.json?214214124 var pathname = url.parse(req.url).pathname; console.log(pathname); if(pathname == '/'){ pathname = '/index.html'
; /*預設載入的首頁*/ } //獲取檔案的字尾名 var extname = path.extname(pathname); if(pathname != '/favicon.ico'){ /*過濾請求favicon.ico*/ //console.log(pathname); //檔案操作獲取 static下面的index.html fs.readFile('static/' + pathname,function(err,data){ if(err){ /*沒有這個檔案*/ console.log('404'
); fs.readFile('static/404.html',function(error,data404){ if(error){ console.log(error); } res.writeHead(404,{"Content-Type":"text/html;charset='utf-8'"}); res.write(data404); res.end(); /*結束響應*/ }) }else{ /*返回這個檔案*/ var mime = mimeModel.getMime(fs,extname,function(mime){ res.writeHead(200,{"Content-Type":"" + mime + ";charset='utf-8'"}); res.write(data); res.end(); /*結束響應*/ }); } }) } }).listen(8001);

為了避免主程式的程式碼太多太亂,不利於維護,我們需要把我們的一些程式碼封裝到router.js中。

//fs模組

var fs=require('fs');
//path模組
var path=require('path');  /*nodejs自帶的模組*/
//url模組
var url=require('url');

//獲取檔案型別的方法  私有
function getMime(extname,callback){  /*獲取字尾名的方法*/

    fs.readFile('./mime.json',function(err,data){

        if(err){
            console.log('mime.json檔案不存在');
            return false;
        }
        //console.log(data.toString());

        var Mimes=JSON.parse(data.toString());

        var result= Mimes[extname] || 'text/html';

        callback(result)

    })


}

exports.statics=function(req,res,staticpath){


    var pathname=url.parse(req.url).pathname;   /*獲取url的值*/


    if(pathname=='/'){
        pathname='/index.html'; /*預設載入的首頁*/
    }
    //獲取檔案的字尾名
    var extname=path.extname(pathname);

    if(pathname!='/favicon.ico'){  /*過濾請求favicon.ico*/
        //console.log(pathname);
        //檔案操作獲取 static下面的index.html

        fs.readFile(staticpath+'/'+pathname,function(err,data){

            if(err){  /*麼有這個檔案*/

                console.log('404');

                fs.readFile(staticpath+'/404.html',function(error,data404){
                    if(error){
                        console.log(error);
                    }
                    res.writeHead(404,{"Content-Type":"text/html;charset='utf-8'"});
                    res.write(data404);
                    res.end(); /*結束響應*/
                })

            }else{ /*返回這個檔案*/

               getMime(extname,function(mime){
                    res.writeHead(200,{"Content-Type":""+mime+";charset='utf-8'"});
                    res.write(data);
                    res.end(); /*結束響應*/
                });

            }
        })

    }

}

封裝好之後,主程式中就只需要引入router檔案,並呼叫statics方法:


//引入http模組
var http=require('http');

//引入副檔名的方法是在檔案裡面獲取到的。

var router=require('./model/router.js');

//console.log(mimeModel.getMime('.css'));   //獲取檔案型別

http.createServer(function(req,res){

    router.statics(req,res,'static');



}).listen(8001);




這裡寫圖片描述