默認情況下,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);
}
}
}
}
運行后的效果如下:
如果routes有同名的兩個js文件,分屬於不同的文件夾,那會是怎么樣的結果呢?假設dd目錄下也有相同的一個js文件hello.js
exports.autoroute = {
'get' : {
'/hello(/?)' : hello,
'/hello/:id' : hello2
},
'post' : {
}
}
function hello(req, res) {
res.end('dd hello');
}
function hello2(req, res) {
res.end('dd hello ' + req.params.id);
}
運行后的效果:
而控制台上也會顯示當前監聽的url
如此一來,我們的開發就會很方便了。
本文的代碼和思路參考了express-autoroute,在express中使用也比較簡單^_^
安裝:
npm install express-autoroute
使用:
var autorouter = require('express-autoroute');
autorouter(app); //其中app = express();
有興趣的可以讀讀它的源碼,這篇文章本來就是參數它來的:)