简体   繁体   中英

flatiron.js / union - where to put app.use() configuration?

Taking the simple example from Union, I am wondering where I can put configuration code that usually goes in app.configure, like passport.js :

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

Any ideas? server and router don't accept use().

Union supports connect middlewares via the before property, as previously mentioned by others. However, union does not handle application configuration; flatiron does. The api, however, is significantly different from express.

For example, configuring an application may look something like this:

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());

I haven't tried running this example (I'm sure there are more details here) but hopefully this gives you an idea.

I just built a wrapper to integrate Passport.js with Flatiron.js.

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

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

Please read the README.md on how to use it and apply it to your application.

I have tested it on LocalStrategy, but it should work for other strategies.

Please let me know otherwise.

Union appears to use the before collection for this:

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

    // etc.
  ]
});

From the "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`.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM