Initializer (question from a new learner)

123 views
Skip to first unread message

Arjon Buikstra

unread,
Dec 2, 2013, 7:57:12 AM12/2/13
to locomo...@googlegroups.com
Hey guys,

I'm getting my hands dirty with Locomotive.js and I'm not completely sure why I need to initialize some components in my environment on top of in the initializer. For example, the passport module has logic in 01_passport.js, which requires 'passport', does a bunch of stuff and declares functions, and exposes it. I read the docs and it said that in the scope of initializer 'this' is the environment. Therefor I expected I could do this at the end of my passport initializer:
  
    this.use(passport.initialize());
    this.use(passport.session());

But then it complains passport is not initialized. The solution is to add these lines, and a seperate require, into all.js.

I'm not sure why? Isn't 'this' in both cases the same object?

Any help, links, comments, and other enlightenment is greatly appreciated!

Jared Hanson

unread,
Dec 2, 2013, 10:50:34 AM12/2/13
to locomo...@googlegroups.com
There's no real difference between environments and initializers.  Environments are a special case of initializer that runs first, and requires a file based on the environment name.

I've found that I don't use environments, and only use initializers.  For example, I'll have a 01_passport.js which configures strategies, and then a 02_middleware.js where I set up the middleware stack in the proper order.

In Locomotive 0.4.x, the boot phases are exposed to your applications, so if you prefer not to use environments, you can get rid of them.  Details here:





--
You received this message because you are subscribed to the Google Groups "Locomotive" group.
To unsubscribe from this group and stop receiving emails from it, send an email to locomotivejs...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Jared Hanson <http://jaredhanson.net/>

Arjon Buikstra

unread,
Dec 3, 2013, 4:33:48 AM12/3/13
to locomo...@googlegroups.com
Thanks for the response. It brings my neurons into full activity at the start of my day.

I am surprised it is actually possible! I ended the day yesterday with everything configured in my initializers, by use'd in all.js.
So for passport I need this in my all.js:

this.use(passport.initialize());
this.use(passport.session());

Since if those lines are only in 01_passport.js I get this error, even though that initializer is definitely loaded:

Error: passport.initialize() middleware not in use
    at IncomingMessage.req.login.req.logIn (<appdir>\node_modules\passport\lib\passport\http\request.js:30:30)
    at Context.delegate.success (<appdir>\node_modules\passport\lib\passport\middleware\authenticate.js:194:13)
    at Context.actions.success (<appdir>\node_modules\passport\lib\passport\context\http\actions.js:21:25)
    at verified (<appdir>\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:181:18)
    at Strategy._verify (<appdir>\config\initializers\01_passport.js:32:4)
    at <appdir>\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:197:22
    at <appdir>\node_modules\passport-facebook\lib\strategy.js:183:5
    at passBackControl (<appdir>\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:107:9)
    at IncomingMessage.<anonymous> (<appdir>\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:124:7)
    at IncomingMessage.EventEmitter.emit (events.js:117:20) 



I'm probably not understanding properly how to set up my initializer as middleware. I also created a config/middlewares dir for these files. How would you call the init functions on those middlewares after the initializers finish?

I hope my new-to-the-game questions aren't annoying you too much! Thanks for the help.
Reply all
Reply to author
Forward
0 new messages