2012-03-19 148 views
1

以從聯盟simple example,我想知道在那裏我可以把那通常會在app.configure,像passport.js配置代碼:flatiron.js/union - 在哪裏放app.use()配置?

app.configure(function() { 
    // Initialize Passport! Also use passport.session() middleware, to support 
    // persistent login sessions (recommended). 
    app.use(passport.initialize()); 
    app.use(passport.session()); 
}); 

任何想法?服務器和路由器不接受use()。

回答

1

聯盟似乎使用before收集這樣的:

var server = union.createServer({ 
    before: [ 
    connect.session({ secret: 'keyboard cat' }), // for `passport.session()` 
    passport.initialize(), 
    passport.session(), 

    // etc. 
    ] 
}); 

"API" documentation

@option before {Array} 
    The `before` value is an array of middlewares, which are used to route and serve incoming 
    requests. For instance, in the example, `favicon` is a middleware which handles requests 
    for `/favicon.ico`. 
+0

'之前'與每個請求調用。應用程序加載時,僅配置一次app.configure。所以這是不一樣的(併產生大量的開銷)。 – Patrick 2012-03-20 09:39:18

+0

@Patrick這就是[Connect](http://www.senchalabs.org/connect/)基於中間件的工作原理 - 它們必須能夠響應每個請求,或者將它傳遞到下一個中​​間件。 ['app.configure'](http://expressjs.com/guide.html#configuration)設置相同的場景;它只是選擇一系列'app.use'調用(可以選擇將它們連接到各種部署模式),而不是一個單獨的,常量的'before'數組。這就是爲什麼它可以幫助挑剔你使用的中間件 - 每一個都會不斷增加一些開銷。 – 2012-03-20 16:53:19

+0

嗯,好的。我習慣於Express中的場景。我只是想知道,因爲護照不起作用,我不明白爲什麼。 – Patrick 2012-03-20 18:07:15

1

聯盟支持通過before屬性連接的中間件,如前面其他人所說。但是,union不處理應用程序配置;熨斗做。但是,api與express有很大不同。

例如,配置一個應用程序可能是這個樣子:

var path = require('path'), 
    flatiron = require('flatiron'), 
    app = flatiron.app, 
    plugins = flatiron.plugins, 
    connect = require('connect'), // most connect middlewares work with flatiron ootb 
    passport = require('passport'); 

// Use flatiron's http plugin (not the same as a middleware!) 
app.use(plugins.http); 

// configuration consists of key/value pairs, not of function blocks associated with 
// certain "environments". 
// Here's *a* way you can handle environment-based configs; there are others! 
app.config.file(path.resolve(
    __dirname, 
    'config', 
    (process.env.NODE_ENV || 'config') + '.json' 
)); 

// Use our config to set the secret 
app.http.before.push(connect.session({ 
    secret: app.config.get('secret') || 'keyboard cat' //default 
})) 
app.http.before.push(passport.initialize()); 
app.http.before.push(passport.session()); 

我還沒有試過運行這個例子(我敢肯定有很多細節),但希望這給你的想法。

1

我剛建立了一個包裝器,將Passport.js和Flatiron.js集成在一起。

https://npmjs.org/package/flatiron-passport

https://github.com/travist/flatiron-passport

請閱讀如何使用它的README.md並將其應用到您的應用程序。

我在LocalStrategy上測試過它,但它應該適用於其他策略。

請讓我知道否則。

+0

感謝您發佈您的答案!請務必仔細閱讀[自助推廣常見問題](http://stackoverflow.com/faq#promotion)。 – 2012-08-28 12:42:54

+1

我不推銷任何東西,除了我寫的一個自由和開放源代碼軟件包來解決這個人關於整合passport.js與flatiron.js的問題。我還透露,我寫了它。自我推銷指南禁止我提供代碼鏈接,或者我應該將其直接複製並粘貼到我的答案中。對不起......剛剛提供給stackoverflow,所以不完全確定我違反了條款。 – travist 2012-08-28 19:49:12

+0

您是否看過我發佈的鏈接?有些東西是免費的或開源的,在這裏沒有任何影響。我不是說你侵犯了任何東西。我正在強烈建議你閱讀和理解。 – 2012-08-28 19:54:36