express中路由配置优化
默認(rèn)情況下,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);
這樣寫是什么大的問題,至少它能正常運(yùn)行。但有二個小問題:
1、不便于以后的擴(kuò)展和維護(hù),如果需要監(jiān)聽的URL越來越多,它看上去就會越來越糟糕了;
2、不夠靈活,如果還需要對“hello”、“aa”…進(jìn)行監(jiān)聽,所有的代碼處理都被放在一個地方了,只會越來越臃腫;
?
有沒有比較好的方案對路由的處理管理和配置呢?
我們可以使用“慣例優(yōu)先原則”來定義好一個約定:我期望的是所有的路由相關(guān)的處理,都放在項目文件路徑下,一個名為“routes”的文件夾里,里面可以可以再建立N層文件夾,而每一個js文件,僅處理以該文件名為路徑的請求,如上面的“blacklists”相關(guān)的處理全部放在blacklists.js文件內(nèi)。如何實(shí)現(xiàn)呢?
?
1、獲取當(dāng)前項目routes目錄內(nèi)所有的文件(包含子文件夾);
2、加載每一個文件,獲取指定屬性下的配置,如果存在,就動態(tài)拼接成如文章開頭的配置,如:app[method](path, func);
?
部分實(shí)現(xiàn)代碼:
//假設(shè)文件存放在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); } ------------------------------------------------------------------------ //當(dāng)前項目的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()) {//遞歸執(zhí)行函數(shù)} else {//加載文件并解析loadFile(filePath);}})}); })function loadFile(filePath) {var routeObj = require(filePath);//如果包含autoroute屬性,則進(jìn)行解析if (routeObj.autoroute) {/** autoroute就是上面hello.js的內(nèi)容:'get' : {'/hello(/?)' : hello,'/hello/:id' : hello2},'post' : {}*/for (var method in routeObj.autoroute) {var routeList = routeObj.autoroute[method];if (!routeList) {break ;}//method就是上面取到的get、postfor(var routeRule in routeList) {//func獲取得到的就是上面對應(yīng)各項的處理函數(shù)var func = routeList[path];app[method](routeRule, func);}}} }原文地址:http://www.cnblogs.com/meteoric_cry/archive/2013/02/19/2917639.html
轉(zhuǎn)載于:https://www.cnblogs.com/ferryInJs/p/4604998.html
總結(jié)
以上是生活随笔為你收集整理的express中路由配置优化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 横向排列两个多个div盒子的方法(CSS
- 下一篇: 自定义存储过程和函数