2013-06-28 38 views
0

有誰知道如何實現connect/express中使用的函數鏈。因此,人們可以做到這一點..NodeJS Connect/Express app.use處理

var app = {}, app.stack = []; 
app.use(function(r, s, n){ 
    // dosomething 
}) 

require('http').createServer(function(r, s){ 
    // execute functions in app stack 
}) 
+0

您想重新創建Connect/Express的中間件堆棧嗎?如果是這樣:爲什麼? – robertklep

+0

教育。那func鏈似乎是一個有用的模式.. –

+1

那麼,一個好的起點是['connect/lib/proto.js'](https://github.com/senchalabs/connect/blob/master/lib/proto。 js),其中'use'和'handle'被實現。 – robertklep

回答

1

中間件「鏈」,其實只是一個「堆棧」,這實際上是一個函數,以執行一個簡單的JavaScript數組。無論何時您撥打use,連接將您的功能附加到中間件堆棧。當需要運行中間件時,連接只需按照邏輯順序執行所有功能即可通過req, res, next參數並連線next回調意味着繼續處理中間件堆棧。我同意@robertklep,你應該閱讀源代碼,因爲它非常易讀和明瞭。