express中路由配置優化


默認情況下,express的路由寫起來還挺麻煩的。比如下面這樣:

app.get('/blacklists/', get_all); 
app.get('/blacklists/:id', get_all); 
app.post('/blacklists/:id', update); 
app.post('/blacklists', create); 
app.detete('/blacklists/:id' : del);

  

這樣寫是什么大的問題,至少它能正常運行。但有二個小問題:

1、不便於以后的擴展和維護,如果需要監聽的URL越來越多,它看上去就會越來越糟糕了;

2、不夠靈活,如果還需要對“hello”、“aa”…進行監聽,所有的代碼處理都被放在一個地方了,只會越來越臃腫;

 

有沒有比較好的方案對路由的處理管理和配置呢?

我們可以使用“慣例優先原則”來定義好一個約定:我期望的是所有的路由相關的處理,都放在項目文件路徑下,一個名為“routes”的文件夾里,里面可以可以再建立N層文件夾,而每一個js文件,僅處理以該文件名為路徑的請求,如上面的“blacklists”相關的處理全部放在blacklists.js文件內。如何實現呢?

 

1、獲取當前項目routes目錄內所有的文件(包含子文件夾);

2、加載每一個文件,獲取指定屬性下的配置,如果存在,就動態拼接成如文章開頭的配置,如:app[method](path, func);

 

部分實現代碼:

//假設文件存放在routes目錄,取名為hello.js
exports.autoroute = {
    'get' : {
        '/hello(/?)' : hello,
        '/hello/:id' : hello2
    },
    'post' : {

    }
}

function hello(req, res) {
    res.end('hello');
}

function hello2(req, res) {
    res.end('hello ' + req.params.id);
}
------------------------------------------------------------------------
//當前項目的routes目錄的路徑
var routesDir = path.dirname(require.main.filename) + "/routes";

fs.readdir(routesDir, function(err, files) {
    if (err) {
        return ;
    }

    files.forEach(function(path) {
        //routes目錄下的文件路徑
        var filePath = routesDir + "/" + path;
        
        fs.stat(filePath, function(err, stats) {
            if (err) {
                return ;    
            }

            if (stats.isDirectory()) {
                //遞歸執行函數
            } else  {
                //加載文件並解析
                    loadFile(filePath);
            }
        })
    });
})


function loadFile(filePath) {
    var routeObj = require(filePath);
    
    //如果包含autoroute屬性,則進行解析
    if (routeObj.autoroute) {
        /*
         * autoroute就是上面hello.js的內容:
            'get' : {
               '/hello(/?)' : hello,
               '/hello/:id' : hello2
           },
           'post' : {
        
           }
         */
        for (var method in routeObj.autoroute) {
            var routeList = routeObj.autoroute[method];
            
            if (!routeList) {
                break ;
            }
            
            //method就是上面取到的get、post

            for(var routeRule in routeList) {
                //func獲取得到的就是上面對應各項的處理函數
                var func = routeList[path];

                app[method](routeRule, func);
            }
        }
    }
}

  原文地址:http://www.cnblogs.com/meteoric_cry/archive/2013/02/19/2917639.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM