1

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

Patrick
  • 7,903
  • 11
  • 52
  • 87

3 Answers3

1

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.

Josh Holbrook
  • 1,611
  • 13
  • 10
1

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.

travist
  • 11
  • 1
  • Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. – Andrew Barber Aug 28 '12 at 12:42
  • 1
    I am not promoting anything other than a free and open source package that I wrote to solve this persons question regarding integrating passport.js with flatiron.js. I also disclosed that I wrote it. Do the self-promotion guidelines prohibit me from providing links to code, or should I copy and paste it directly to my answer. Sorry... just new to contributing to stackoverflow, so not completely sure how I violated terms. – travist Aug 28 '12 at 19:49
  • Did you read the link I posted? That something is free or open source has no bearing whatever here. I'm not saying you violated anything. I'm making a strong suggestion you read and understand. – Andrew Barber Aug 28 '12 at 19:54
1

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`.
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • 'before' is called with every request. app.configure only once when the app loads. So that's not the same (and produces a lot of overhead). – Patrick Mar 20 '12 at 09:39
  • @Patrick That's how [Connect](http://www.senchalabs.org/connect/)-based middleware work -- they have to be able to respond to each request, or they pass it onto the next middleware. [`app.configure`](http://expressjs.com/guide.html#configuration) sets up the same scenario; it just opts for a series of `app.use` calls (with the option of attaching them to various deployment modes) rather than a single, constant `before` array. This is why it can help to be picky about the middleware you use -- each one will invariably add some overhead. – Jonathan Lonowski Mar 20 '12 at 16:53
  • Hmm, ok. I am used to the scenario in Express. I just wonder, as Passport doesn't work, and I can't figure out why. – Patrick Mar 20 '12 at 18:07